package com.xoa.service.hr.impl;

import com.xoa.dao.common.HrCodeMapper;
import com.xoa.dao.common.SysCodeMapper;
import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.duties.DutiesManagementMapper;
import com.xoa.dao.hr.HrStaffInfoMapper;
import com.xoa.dao.hr.HrStaffLeaveMapper;
import com.xoa.dao.position.PositionManagementMapper;
import com.xoa.dao.users.UserDeptOrderMapper;
import com.xoa.dao.users.UserPrivMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.common.HrCode;
import com.xoa.model.common.Syslog;
import com.xoa.model.duties.UserPost;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.hr.HrStaffInfo;
import com.xoa.model.hr.HrStaffInfoWithBLOBs;
import com.xoa.model.hr.HrStaffLeave;
import com.xoa.model.modulePriv.ModulePriv;
import com.xoa.model.position.UserJob;
import com.xoa.model.users.UserDeptOrder;
import com.xoa.model.users.UserExt;
import com.xoa.model.users.UserPriv;
import com.xoa.model.users.Users;
import com.xoa.service.hr.HrSStaffInfoService;
import com.xoa.service.sys.SysLogService;
import com.xoa.service.users.UsersService;
import com.xoa.util.*;
import com.xoa.util.common.L;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.common.wrapper.BaseWrappers;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import static com.xoa.util.FileUploadUtil.allowUpload;

/**
 * Created by 韩东堂 on 2017/7/12.
 */
@Service
public class HrStaffInfoServiceImpl implements HrSStaffInfoService {
    //注入mybatis
    @Resource
    private HrStaffInfoMapper hrStaffInfoMapper;
    @Resource
    private UsersMapper usersMapper;
    @Resource
    private DepartmentMapper departmentMapper;
    @Resource
    private UsersService usersService;
    @Resource
    private HrStaffLeaveMapper hrStaffLeaveMapper;

    @Resource
    private UserPrivMapper userPrivMapper;

    @Autowired
    private HrCodeMapper hrCodeMapper;
    @Resource
    private DutiesManagementMapper dutiesManagementMapper;
    @Resource
    private PositionManagementMapper positionManagementMapper;

    @Resource
    private SysCodeMapper sysCodeMapper;

    @Resource
    UserDeptOrderMapper userDeptOrderMapper;

    @Autowired
    private SysLogService sysLogService;
    /**
     * 查询退休人员信息
     *
     * @param startTime
     * @param endTime
     * @return
     */
    @Override
    public BaseWrappers selectRetireeInfo(String startTime, String endTime) {
        BaseWrappers wrapper = new BaseWrappers();
        //实现查询功能  （注意参数 时间满不满足格式化条件  例如 给了一个 2017-33-44 明显不对）
       /* Date dt=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        String data=format.format(dt);
        wrapper.getTime();*/
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        try {
            /*DateFormat.getDate(startTime);
            DateFormat.getDate(endTime);*/
            Date startDate = format.parse(startTime);
            Date endDate = format.parse(endTime);
            HashMap<String, Date> map = new HashMap<>();
            map.put("endDate", endDate);
            map.put("startDate", startDate);
            List<HrStaffInfo> hrStaffInfosList = hrStaffInfoMapper.selectRetireeInfo(map);
            wrapper.setDatas(hrStaffInfosList);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        wrapper.setStatus(true);
        wrapper.setFlag(true);
        wrapper.setMsg("退休人员");
        return wrapper;
    }


    @Override
    public ToJson<HrStaffInfo> selectPersonFileByDeptId(HttpServletRequest request, Integer deptId) {
        /*根据deptId获取人员列表信息*/
        ToJson<HrStaffInfo> toJson = new ToJson<HrStaffInfo>(1, "error");
        try {
            List<HrStaffInfo> list = hrStaffInfoMapper.selectPersonFileByDeptId(deptId);
        /*for(HrStaffInfo hrStaffInfo:list){
            if(hrStaffInfo.getStaffBirth()!=null){
                String staff=DateFormat.getStrTime(Integer.valueOf(DateFormat.getStrDate(hrStaffInfo.getStaffBirth())));
                hrStaffInfo.setStaffBirth(DateFormat.getDate(staff));
            }
        }*/
            if (list.size() > 0) {
                for (HrStaffInfo hr : list) {
                    //籍贯
                    HrCode hr_staff_contract2 = hrCodeMapper.selectCode("AREA", hr.getStaffNativePlace());
                    if (hr_staff_contract2 != null) {
                        hr.setStaffNativePlaceName(hr_staff_contract2.getCodeName());
                    } else {
                        hr.setStaffNativePlaceName(" ");
                    }
                }
                toJson.setObj(list);
                toJson.setMsg("ok");

            } else {
                toJson.setMsg("人员档案信息为空");
            }
            toJson.setFlag(0);
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            L.e("HrRetireeServiceImpl selectPersonFileByDeptId:" + e);
        }
        return toJson;
    }


    @Override
    public HrStaffInfo selectPersonFileByUserId(HttpServletRequest request, Integer uid) {
        /*根据userId查询人员的详细信息*/
        ToJson<HrStaffInfo> toJson = new ToJson<HrStaffInfo>(1, "error");
        Users users = usersMapper.selectUserByUId(uid);
        String userId = users.getUserId();
        HrStaffInfo hrStaffInfo = hrStaffInfoMapper.getStaffByUserId(userId);
        if (hrStaffInfo != null && "02".equals(hrStaffInfo.getWorkStatus())) {
            HrStaffLeave hrStaffLeave = hrStaffLeaveMapper.queryStaffLeaveByLeavePerson(userId);
            if (hrStaffLeave != null) {
                hrStaffInfo.setOuitTimeFact(hrStaffLeave.getQuitTimeFact());
            }
        }
        try {
            if (hrStaffInfo == null) {
                hrStaffInfo = new HrStaffInfo();
                hrStaffInfo.setUid(users.getUid());
                hrStaffInfo.setUserId(users.getUserId());
                hrStaffInfo.setStaffName(users.getUserName());
                hrStaffInfo.setDeptId(users.getDeptId());
                if (users.getDeptId() != null) {
                    if (!StringUtils.checkNull(departmentMapper.getDeptNameByDeptId(users.getDeptId()))) {
                        hrStaffInfo.setDeptName(departmentMapper.getDeptNameByDeptId(users.getDeptId()));
                    }
                }
                hrStaffInfo.setUserPriv(users.getUserPriv().toString());
                hrStaffInfo.setUserPrivName(users.getUserPrivName());
                hrStaffInfo.setStaffSex(users.getSex());
                hrStaffInfo.setStaffBirth(users.getBirthdayStr());
                hrStaffInfo.setIsLunar(users.getIsLunar());//是否农历
                hrStaffInfo.setHomeAddress(users.getAddHome());
                hrStaffInfo.setStaffPhone(users.getTelNoHome());
                hrStaffInfo.setStaffMobile(users.getMobilNo());
                hrStaffInfo.setStaffEmail(users.getEmail());
                hrStaffInfo.setStaffQq(users.getOicqNo());
                hrStaffInfo.setStaffMsn(users.getMsn());
                hrStaffInfo.setPhoto(users.getPhoto());
                hrStaffInfo.setByName(users.getByname());
                hrStaffInfo.setWorkJob(String.valueOf(users.getJobId()));
                hrStaffInfo.setJobPosition(String.valueOf(users.getPostId()));
                if(hrStaffInfo.getProjectName()!=null &&!"".equals(hrStaffInfo.getProjectName())){
                    hrStaffInfo.setProjectNAME(sysCodeMapper.getCodeName(hrStaffInfo.getProjectName()));
                }
                if(hrStaffInfo.getLocationAddress()!=null &&!"".equals(hrStaffInfo.getLocationAddress())){
                    hrStaffInfo.setLocationAddressName(sysCodeMapper.getCodeName(hrStaffInfo.getLocationAddress()));
                }

            } else {
                if (hrStaffInfo.getDeptId() != null) {
                    hrStaffInfo.setDeptName(departmentMapper.getDeptNameById(hrStaffInfo.getDeptId()));
                }
                Users usersTemp = usersMapper.getUsersByuserId(hrStaffInfo.getUserId());
                hrStaffInfo.setUserPriv(usersTemp.getUserPriv().toString());
                if (usersTemp.getUserPriv() != null) {
                    hrStaffInfo.setUserPrivName(userPrivMapper.getPrivNameById(Integer.valueOf(usersTemp.getUserPriv())));
                }
                hrStaffInfo.setByName(usersTemp.getByname());
                hrStaffInfo.setUid(uid);
                //直属上级和直属下级
                StringBuffer superiorName = new StringBuffer();
                if (!StringUtils.checkNull(hrStaffInfo.getDirectlySuperior())) {
                    String[] superiorArray = hrStaffInfo.getDirectlySuperior().split(",");
                    for (String superior : superiorArray) {
                        if (!StringUtils.checkNull(superior)) {
                            if (usersMapper.getUserByUid(Integer.valueOf(superior)) != null) {
                                superiorName.append(usersMapper.getUserByUid(Integer.valueOf(superior)).getUserName() + ",");
                            }
                        }
                    }
                }
                hrStaffInfo.setDirectlySuperiorName(superiorName.toString());
                StringBuffer underName = new StringBuffer();
                if (!StringUtils.checkNull(hrStaffInfo.getDirectlyUnder())) {
                    String[] underArray = hrStaffInfo.getDirectlyUnder().split(",");
                    for (String under : underArray) {
                        if (!StringUtils.checkNull(under)) {
                            if (usersMapper.getUserByUid(Integer.valueOf(under)) != null) {
                                underName.append(usersMapper.getUserByUid(Integer.valueOf(under)).getUserName() + ",");
                            }
                        }
                    }
                }
                hrStaffInfo.setDirectlyUnderName(underName.toString());

            }
            //附件处理
            List<Attachment> attachmentList = new ArrayList<Attachment>();
            if (hrStaffInfo.getAttachmentName() != null && !"".equals(hrStaffInfo.getAttachmentName())) {
                attachmentList = GetAttachmentListUtil.returnAttachment(hrStaffInfo.getAttachmentName(), hrStaffInfo.getAttachmentId(), "xoa" + (String) request.getSession().getAttribute("loginDateSouse"), GetAttachmentListUtil.MODULE_HRSTAFFINFO);
            }
            hrStaffInfo.setAttachmentList(attachmentList);

            toJson.setObject(hrStaffInfo);
            toJson.setMsg("ok");
            toJson.setFlag(0);
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return hrStaffInfo;
    }

    /**
     * 创建作者: 季佳伟
     * 方法介绍：根据userId查询人员的详细信息（用ToJson）
     *
     * @param request
     * @param uid
     * @return
     */
    public ToJson<HrStaffInfo> selectPersonByUserId(HttpServletRequest request, Integer uid) {
        /*根据userId查询人员的详细信息*/
        ToJson<HrStaffInfo> toJson = new ToJson<HrStaffInfo>(1, "error");
        Users users = usersMapper.selectUserByUId(uid);
        String userId = users.getUserId();
        HrStaffInfo hrStaffInfo = hrStaffInfoMapper.getStaffByUserId(userId);
        try {
            if (hrStaffInfo == null) {
                hrStaffInfo = new HrStaffInfo();
                hrStaffInfo.setUid(users.getUid());
                hrStaffInfo.setUserId(users.getUserId());
                hrStaffInfo.setStaffName(users.getUserName());
                hrStaffInfo.setDeptId(users.getDeptId());
                if (users.getDeptId() != null) {
                    if (!StringUtils.checkNull(departmentMapper.getDeptNameByDeptId(users.getDeptId()))) {
                        hrStaffInfo.setDeptName(departmentMapper.getDeptNameByDeptId(users.getDeptId()));
                    }
                }
                hrStaffInfo.setUserPriv(users.getUserPriv().toString());
                hrStaffInfo.setUserPrivName(users.getUserPrivName());
                hrStaffInfo.setStaffSex(users.getSex());
                hrStaffInfo.setStaffBirth(users.getBirthdayStr());
                hrStaffInfo.setIsLunar(users.getIsLunar());//是否农历
                hrStaffInfo.setHomeAddress(users.getAddHome());
                hrStaffInfo.setStaffPhone(users.getTelNoHome());
                hrStaffInfo.setStaffMobile(users.getMobilNo());
                hrStaffInfo.setStaffEmail(users.getEmail());
                hrStaffInfo.setStaffQq(users.getOicqNo());
                hrStaffInfo.setStaffMsn(users.getMsn());
                hrStaffInfo.setPhoto(users.getPhoto());

            } else {
                if (hrStaffInfo.getDeptId() != null) {
                    hrStaffInfo.setDeptName(departmentMapper.getDeptNameById(hrStaffInfo.getDeptId()));
                }
                Users usersTemp = usersMapper.getUsersByuserId(hrStaffInfo.getUserId());
                hrStaffInfo.setUserPriv(usersTemp.getUserPriv().toString());
                if (usersTemp.getUserPriv() != null) {
                    hrStaffInfo.setUserPrivName(userPrivMapper.getPrivNameById(Integer.valueOf(usersTemp.getUserPriv())));
                }
                hrStaffInfo.setUid(uid);
                //直属上级和直属下级
                StringBuffer superiorName = new StringBuffer();
                if (!StringUtils.checkNull(hrStaffInfo.getDirectlySuperior())) {
                    String[] superiorArray = hrStaffInfo.getDirectlySuperior().split(",");
                    for (String superior : superiorArray) {
                        if (!StringUtils.checkNull(superior)) {
                            if (usersMapper.getUserByUid(Integer.valueOf(superior)) != null) {
                                superiorName.append(usersMapper.getUserByUid(Integer.valueOf(superior)).getUserName() + ",");
                            }
                        }
                    }
                }
                hrStaffInfo.setDirectlySuperiorName(superiorName.toString());
                StringBuffer underName = new StringBuffer();
                if (!StringUtils.checkNull(hrStaffInfo.getDirectlyUnder())) {
                    String[] underArray = hrStaffInfo.getDirectlyUnder().split(",");
                    for (String under : underArray) {
                        if (!StringUtils.checkNull(under)) {
                            if (usersMapper.getUserByUid(Integer.valueOf(under)) != null) {
                                underName.append(usersMapper.getUserByUid(Integer.valueOf(under)).getUserName() + ",");
                            }
                        }
                    }
                }
                hrStaffInfo.setDirectlyUnderName(underName.toString());

            }
            //附件处理
            List<Attachment> attachmentList = new ArrayList<Attachment>();
            if (hrStaffInfo.getAttachmentName() != null && !"".equals(hrStaffInfo.getAttachmentName())) {
                attachmentList = GetAttachmentListUtil.returnAttachment(hrStaffInfo.getAttachmentName(), hrStaffInfo.getAttachmentId(), "xoa" + (String) request.getSession().getAttribute("loginDateSouse"), GetAttachmentListUtil.MODULE_HRSTAFFINFO);
            }
            hrStaffInfo.setAttachmentList(attachmentList);

            toJson.setObject(hrStaffInfo);
            toJson.setMsg("ok");
            toJson.setFlag(0);
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    /**
     * 创建作者:   季佳伟
     * 创建日期:   2017年11月17日 上午11:10:00
     * 方法介绍:   查询登录人的人事档案
     *
     * @param request
     * @param user
     * @return
     */
    public ToJson<HrStaffInfo> selectOwnStaffInfo(HttpServletRequest request, Users user) {
        ToJson<HrStaffInfo> toJson = new ToJson<HrStaffInfo>(1, "error");
        Users users = usersMapper.selectUserByUId(user.getUid());
        String userId = users.getUserId();
        HrStaffInfo hrStaffInfo = hrStaffInfoMapper.getStaffByUserId(userId);
        try {
            if (hrStaffInfo == null) {
                hrStaffInfo = new HrStaffInfo();
                hrStaffInfo.setUid(users.getUid());
                hrStaffInfo.setUserId(users.getUserId());
                hrStaffInfo.setStaffName(users.getUserName());
                hrStaffInfo.setDeptId(users.getDeptId());
                if (users.getDeptId() != null) {
                    if (!StringUtils.checkNull(departmentMapper.getDeptNameByDeptId(users.getDeptId()))) {
                        hrStaffInfo.setDeptName(departmentMapper.getDeptNameByDeptId(users.getDeptId()));
                    }
                }
                hrStaffInfo.setUserPriv(users.getUserPriv().toString());
                hrStaffInfo.setUserPrivName(users.getUserPrivName());
                hrStaffInfo.setStaffSex(users.getSex());
                hrStaffInfo.setStaffBirth(users.getBirthdayStr());
                hrStaffInfo.setIsLunar(users.getIsLunar());//是否农历
                hrStaffInfo.setHomeAddress(users.getAddHome());
                hrStaffInfo.setStaffPhone(users.getTelNoHome());
                hrStaffInfo.setStaffMobile(users.getMobilNo());
                hrStaffInfo.setStaffEmail(users.getEmail());
                hrStaffInfo.setStaffQq(users.getOicqNo());
                hrStaffInfo.setStaffMsn(users.getMsn());
                hrStaffInfo.setPhoto(users.getPhoto());

            } else {
                if (hrStaffInfo.getDeptId() != null) {
                    hrStaffInfo.setDeptName(departmentMapper.getDeptNameById(hrStaffInfo.getDeptId()));
                }
                Users usersTemp = usersMapper.getUsersByuserId(hrStaffInfo.getUserId());
                hrStaffInfo.setUserPriv(usersTemp.getUserPriv().toString());
                if (usersTemp.getUserPriv() != null) {
                    hrStaffInfo.setUserPrivName(userPrivMapper.getPrivNameById(Integer.valueOf(usersTemp.getUserPriv())));
                }
                hrStaffInfo.setUid(user.getUid());
                //直属上级和直属下级
                StringBuffer superiorName = new StringBuffer();
                if (!StringUtils.checkNull(hrStaffInfo.getDirectlySuperior())) {
                    String[] superiorArray = hrStaffInfo.getDirectlySuperior().split(",");
                    for (String superior : superiorArray) {
                        if (!StringUtils.checkNull(superior)) {
                            if (usersMapper.getUserByUid(Integer.valueOf(superior)) != null) {
                                superiorName.append(usersMapper.getUserByUid(Integer.valueOf(superior)).getUserName() + ",");
                            }
                        }
                    }
                }
                hrStaffInfo.setDirectlySuperiorName(superiorName.toString());
                StringBuffer underName = new StringBuffer();
                if (!StringUtils.checkNull(hrStaffInfo.getDirectlyUnder())) {
                    String[] underArray = hrStaffInfo.getDirectlyUnder().split(",");
                    for (String under : underArray) {
                        if (!StringUtils.checkNull(under)) {
                            if (usersMapper.getUserByUid(Integer.valueOf(under)) != null) {
                                underName.append(usersMapper.getUserByUid(Integer.valueOf(under)).getUserName() + ",");
                            }
                        }
                    }
                }
                hrStaffInfo.setDirectlyUnderName(underName.toString());

            }
            //附件处理
            List<Attachment> attachmentList = new ArrayList<Attachment>();
            if (hrStaffInfo.getAttachmentName() != null && !"".equals(hrStaffInfo.getAttachmentName())) {
                attachmentList = GetAttachmentListUtil.returnAttachment(hrStaffInfo.getAttachmentName(), hrStaffInfo.getAttachmentId(), "xoa" + (String) request.getSession().getAttribute("loginDateSouse"), GetAttachmentListUtil.MODULE_HRSTAFFINFO);
            }
            hrStaffInfo.setAttachmentList(attachmentList);

            toJson.setObject(hrStaffInfo);
            toJson.setMsg("ok");
            toJson.setFlag(0);
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    @Override
    public BaseWrappers getHrStaffInfoNews() {
        BaseWrappers baseWrapper = new BaseWrappers();
        Calendar cal = Calendar.getInstance();
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH) + 1;
        List<HrStaffInfo> hrStaffInfos = hrStaffInfoMapper.getHrStaffInfoNews(String.valueOf(month), String.valueOf(year));
        baseWrapper.setDatas(hrStaffInfos);
        baseWrapper.setFlag(true);
        baseWrapper.setStatus(true);
        baseWrapper.setMsg("更新成功");
        return baseWrapper;
    }

    @Override
    public BaseWrapper getHrStaffInfoCount(String type) throws ParseException {
        BaseWrapper baseWrapper = new BaseWrapper();
        List<HrStaffInfo> hrStaffInfos = hrStaffInfoMapper.getHrStaffInfoCount();
        SimpleDateFormat myFormatter = new SimpleDateFormat("yyyy-MM-dd");
        if ("1".equals(type)) {
            Map<String, Integer> map = new LinkedHashMap<String, Integer>();
            map.put("20-25岁", 0);
            map.put("25-30岁", 0);
            map.put("30-35岁", 0);
            map.put("35-40岁", 0);
            map.put("40-45岁", 0);
            map.put("45-50岁", 0);
            map.put("50-55岁", 0);
            map.put("55以上", 0);
            for (HrStaffInfo hrStaffInfo : hrStaffInfos) {
                try {
                    //按照出生日期
                    String staffBirth = hrStaffInfo.getStaffBirth();
                    if (staffBirth.equals("0000-00-00")){
                        continue;
                    }
                    Date date = myFormatter.parse(hrStaffInfo.getStaffBirth());
                    int ageNum = DateCompute.getAge(date);
                    if (ageNum >= 20 && ageNum < 25) {
                        map.put("20-25岁", map.get("20-25岁") + 1);
                    } else if (ageNum >= 25 && ageNum < 30) {
                        map.put("25-30岁", map.get("25-30岁") + 1);
                    } else if (ageNum >= 30 && ageNum < 35) {
                        map.put("30-35岁", map.get("30-35岁") + 1);
                    } else if (ageNum >= 35 && ageNum < 40) {
                        map.put("35-40岁", map.get("35-40岁") + 1);
                    } else if (ageNum >= 40 && ageNum < 45) {
                        map.put("40-45岁", map.get("40-45岁") + 1);
                    } else if (ageNum >= 45 && ageNum < 50) {
                        map.put("45-50岁", map.get("45-50岁") + 1);
                    } else if (ageNum >= 50 && ageNum < 55) {
                        map.put("50-55岁", map.get("50-55岁") + 1);
                    } else if (ageNum > 55) {
                        map.put("55以上", map.get("55以上") + 1);
                    }
                } catch (ParseException e) {
                    e.printStackTrace();
                }
            }
            baseWrapper.setData(map);
        } else if ("2".equals(type)) {
            Map<String, Integer> map = new LinkedHashMap<String, Integer>();
            map.put("男", 0);
            map.put("女", 0);
            for (HrStaffInfo hrStaffInfo : hrStaffInfos) {
                if ("1".equals(hrStaffInfo.getSex())) {
                    map.put("女", map.get("女") + 1);
                } else {
                    map.put("男", map.get("男") + 1);
                }
            }
            baseWrapper.setData(map);

        } else if ("3".equals(type)) {
            Map<String, Integer> map = new LinkedHashMap<String, Integer>();
            map.put("小学", 0);
            map.put("初中", 0);
            map.put("高中", 0);
            map.put("中专", 0);
            map.put("大专", 0);
            map.put("本科", 0);
            map.put("研究生", 0);
            map.put("博士", 0);
            map.put("博士后", 0);
            map.put("技校", 0);
            for (HrStaffInfo hrStaffInfo : hrStaffInfos) {
                if ("1".equals(hrStaffInfo.getStaffHighestSchool())) {
                    map.put("小学", map.get("小学") + 1);
                } else if ("2".equals(hrStaffInfo.getStaffHighestSchool())) {
                    map.put("初中", map.get("初中") + 1);
                } else if ("3".equals(hrStaffInfo.getStaffHighestSchool())) {
                    map.put("高中", map.get("高中") + 1);
                } else if ("4".equals(hrStaffInfo.getStaffHighestSchool())) {
                    map.put("中专", map.get("中专") + 1);
                } else if ("5".equals(hrStaffInfo.getStaffHighestSchool())) {
                    map.put("大专", map.get("大专") + 1);
                } else if ("6".equals(hrStaffInfo.getStaffHighestSchool())) {
                    map.put("本科", map.get("本科") + 1);
                } else if ("7".equals(hrStaffInfo.getStaffHighestSchool())) {
                    map.put("研究生", map.get("研究生") + 1);
                } else if ("8".equals(hrStaffInfo.getStaffHighestSchool())) {
                    map.put("博士", map.get("博士") + 1);
                } else if ("9".equals(hrStaffInfo.getStaffHighestSchool())) {
                    map.put("博士后", map.get("博士后") + 1);
                } else if ("11".equals(hrStaffInfo.getStaffHighestSchool())) {
                    map.put("技校", map.get("技校") + 1);
                }
            }
            baseWrapper.setData(map);

        } else if ("4".equals(type)) {
            Map<String, Integer> map = new LinkedHashMap<String, Integer>();
            map.put("1年以内", 0);
            map.put("1-3年", 0);
            map.put("3-5年", 0);
            map.put("5-10年", 0);
            map.put("10年以上", 0);
            for (HrStaffInfo hrStaffInfo : hrStaffInfos) {
                String staffBirth = hrStaffInfo.getDatesEmployed();
                if (staffBirth.equals("0000-00-00")){
                    continue;
                }

                Date date = myFormatter.parse(hrStaffInfo.getDatesEmployed());
                Integer ageNum = DateCompute.getAge(date);
                if (ageNum < 1) {
                    map.put("1年以内", map.get("1年以内") + 1);
                } else if (ageNum >= 1 && ageNum < 3) {
                    map.put("1-3年", map.get("1-3年") + 1);
                } else if (ageNum >= 3 && ageNum < 5) {
                    map.put("3-5年", map.get("3-5年") + 1);
                } else if (ageNum >= 5 && ageNum < 10) {
                    map.put("5-10年", map.get("5-10年") + 1);
                } else if (ageNum >= 10) {
                    map.put("10年以上", map.get("10年以上") + 1);
                }
            }
            baseWrapper.setData(map);
        }


        baseWrapper.setFlag(true);
        baseWrapper.setStatus(true);
        baseWrapper.setMsg("更新成功");
        return baseWrapper;
    }

    /**
     * 条件查询
     *
     * @param hrStaffInfo
     * @param request
     * @return
     */
    @Override
    public ToJson<HrStaffInfo> selectOnLinePerson(HrStaffInfo hrStaffInfo, HttpServletResponse response, HttpServletRequest request, String export) {
        /*查询人事档案人员的信息*/
        ToJson<HrStaffInfo> toJson = new ToJson<HrStaffInfo>(1, "error");
        if (export == null) {
            export = "0";
        }
        try {
            String leaveShow = request.getParameter("leaveShow");
            List<HrStaffInfo> list = new ArrayList<HrStaffInfo>();
            if (StringUtils.checkNull(leaveShow)) {
                list = hrStaffInfoMapper.selectOnLinePerson(hrStaffInfo);
            } else {
                if (!leaveShow.equals("1")) {
                    list = hrStaffInfoMapper.selectOnLinePerson(hrStaffInfo);
                } else {
                    list = hrStaffInfoMapper.selStaffByCondi(hrStaffInfo);
                    for (HrStaffInfo hrStaffInfoTemp : list) {
                        if (!StringUtils.checkNull(hrStaffInfoTemp.getStaffSex())) {
                            if (hrStaffInfoTemp.getStaffSex().equals("1")) {
                                hrStaffInfoTemp.setStaffSexName("女");
                            } else {
                                hrStaffInfoTemp.setStaffSexName("男");
                            }
                        }
                        hrStaffInfoTemp.setStaffPoliticalStatusName("无党派人士");
                        if (!StringUtils.checkNull(hrStaffInfoTemp.getStaffPoliticalStatus())) {
                            if (hrStaffInfoTemp.getStaffPoliticalStatus().equals("1")) {
                                hrStaffInfoTemp.setStaffPoliticalStatusName("群众");
                            } else if (hrStaffInfoTemp.getStaffPoliticalStatus().equals("2")) {
                                hrStaffInfoTemp.setStaffPoliticalStatusName("共青团员");
                            } else if (hrStaffInfoTemp.getStaffPoliticalStatus().equals("3")) {
                                hrStaffInfoTemp.setStaffPoliticalStatusName("中共党员");
                            } else if (hrStaffInfoTemp.getStaffPoliticalStatus().equals("4")) {
                                hrStaffInfoTemp.setStaffPoliticalStatusName("中共预备党员");
                            } else if (hrStaffInfoTemp.getStaffPoliticalStatus().equals("5")) {
                                hrStaffInfoTemp.setStaffPoliticalStatusName("民主党派");
                            } else {
                                hrStaffInfoTemp.setStaffPoliticalStatusName("无党派人士");
                            }
                        }

                        if (!StringUtils.checkNull(hrStaffInfoTemp.getWorkStatus())) {
                            if (hrStaffInfoTemp.getWorkStatus().equals("02")) {
                                hrStaffInfoTemp.setStaffName(hrStaffInfoTemp.getStaffName() + "(该用户已离职)");
                            }
                        }
                    }
                }
            }
            for (HrStaffInfo hr : list) {
                //籍贯
                HrCode hr_staff_contract2 = hrCodeMapper.selectCode("AREA", hr.getStaffNativePlace());
                if (hr_staff_contract2 != null) {
                    hr.setStaffNativePlaceName(hr_staff_contract2.getCodeName());
                } else {
                    hr.setStaffNativePlaceName(" ");
                }
            }
            toJson.setObj(list);
            toJson.setMsg("ok");
            toJson.setFlag(0);
            //判断是否导出
            if (export.equals("1")) {
                HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("人事导出信息表", 9);
                String[] secondTitles = {"Id", "用户名", "用户头像", "档案头像", "OA角色", "性别", "出生年月", "民族", "籍贯", "政治面貌", "入职时间"};
                HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);
                String[] beanProperty = {"staffId", "staffName", "photo", "photoName", "userPrivName", "staffSexName", "staffBirth", "staffNationality", "staffNativePlace2", "staffPoliticalStatusName", "datesEmployed"};
                HSSFWorkbook workbook = ExcelUtil.exportExcelData(workbook2, list, beanProperty);
                OutputStream out = response.getOutputStream();
                String filename = "人事导出信息表.xls";
                filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
                response.setContentType("application/vnd.ms-excel;charset=UTF-8");
                response.setHeader("content-disposition", "attachment;filename=" + filename);
                workbook.write(out);
                out.close();

            }


        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
            toJson.setFlag(1);
        }
        return toJson;
    }

    /**
     * 条件查询
     * 离职
     *
     * @param hrStaffInfo
     * @param request
     * @return
     */
    @Override
    public ToJson<HrStaffInfo> selectLeavePerson(HrStaffInfo hrStaffInfo, HttpServletResponse response, HttpServletRequest request, String export) {
        /*查询人事档案人员的信息*/
        ToJson<HrStaffInfo> toJson = new ToJson<HrStaffInfo>(1, "error");
        if (export == null) {
            export = "0";
        }
        try {
            String leaveShow = request.getParameter("leaveShow");
            List<HrStaffInfo> list = new ArrayList<HrStaffInfo>();
            if (StringUtils.checkNull(leaveShow)) {
                list = hrStaffInfoMapper.selectLeavePerson(hrStaffInfo);
            } else {
                if (!leaveShow.equals("1")) {
                    list = hrStaffInfoMapper.selectLeavePerson(hrStaffInfo);
                } else {
                    list = hrStaffInfoMapper.selStaffByCondi(hrStaffInfo);
                    for (HrStaffInfo hrStaffInfoTemp : list) {
                        if (!StringUtils.checkNull(hrStaffInfoTemp.getStaffSex())) {
                            if (hrStaffInfoTemp.getStaffSex().equals("1")) {
                                hrStaffInfoTemp.setStaffSexName("女");
                            } else {
                                hrStaffInfoTemp.setStaffSexName("男");
                            }
                        }
                        hrStaffInfoTemp.setStaffPoliticalStatusName("无党派人士");
                        if (!StringUtils.checkNull(hrStaffInfoTemp.getStaffPoliticalStatus())) {
                            if (hrStaffInfoTemp.getStaffPoliticalStatus().equals("1")) {
                                hrStaffInfoTemp.setStaffPoliticalStatusName("群众");
                            } else if (hrStaffInfoTemp.getStaffPoliticalStatus().equals("2")) {
                                hrStaffInfoTemp.setStaffPoliticalStatusName("共青团员");
                            } else if (hrStaffInfoTemp.getStaffPoliticalStatus().equals("3")) {
                                hrStaffInfoTemp.setStaffPoliticalStatusName("中共党员");
                            } else if (hrStaffInfoTemp.getStaffPoliticalStatus().equals("4")) {
                                hrStaffInfoTemp.setStaffPoliticalStatusName("中共预备党员");
                            } else if (hrStaffInfoTemp.getStaffPoliticalStatus().equals("5")) {
                                hrStaffInfoTemp.setStaffPoliticalStatusName("民主党派");
                            } else {
                                hrStaffInfoTemp.setStaffPoliticalStatusName("无党派人士");
                            }
                        }

                        if (!StringUtils.checkNull(hrStaffInfoTemp.getWorkStatus())) {
                            if (hrStaffInfoTemp.getWorkStatus().equals("02")) {
                                hrStaffInfoTemp.setStaffName(hrStaffInfoTemp.getStaffName() + "(该用户已离职)");
                            }
                        }
                    }
                }
            }
            toJson.setObj(list);
            toJson.setMsg("ok");
            toJson.setFlag(0);
            //判断是否导出
            if (export.equals("1")) {
                HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("人事导出信息表", 9);
                String[] secondTitles = {"Id", "用户名", "用户头像", "档案头像", "OA角色", "性别", "出生年月", "民族", "籍贯", "政治面貌", "入职时间"};
                HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);
                String[] beanProperty = {"staffId", "staffName", "photo", "photoName", "userPrivName", "staffSexName", "staffBirth", "staffNationality", "staffNativePlace2", "staffPoliticalStatusName", "datesEmployed"};
                HSSFWorkbook workbook = ExcelUtil.exportExcelData(workbook2, list, beanProperty);
                OutputStream out = response.getOutputStream();
                String filename = "人事导出信息表.xls";
                filename = FileUtils.encodeDownloadFilename(filename, request.getHeader("user-agent"));
                response.setContentType("application/vnd.ms-excel;charset=UTF-8");
                response.setHeader("content-disposition", "attachment;filename=" + filename);
                workbook.write(out);
                out.close();

            }


        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
            toJson.setFlag(1);
        }
        return toJson;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ToJson<HrStaffInfo> updatePersonFile(HrStaffInfo hrStaffInfo, HttpServletRequest request) {
        ToJson<HrStaffInfo> toJson = new ToJson<HrStaffInfo>(1, "error");
        try {
            if (hrStaffInfo.getStaffBirth() != null) {
                hrStaffInfo.setStaffBirth(hrStaffInfo.getStaffBirth().trim().equals("") ? "0000-00-00" : hrStaffInfo.getStaffBirth().trim().trim());
            }
            if (hrStaffInfo.getJoinPartyTime() != null) {
                hrStaffInfo.setJoinPartyTime(hrStaffInfo.getJoinPartyTime().trim().equals("") ? "0000-00-00" : hrStaffInfo.getJoinPartyTime().trim().trim());
            }
            if (hrStaffInfo.getJobBeginning() != null) {
                hrStaffInfo.setJobBeginning(hrStaffInfo.getJobBeginning().trim().equals("") ? "0000-00-00" : hrStaffInfo.getJobBeginning().trim().trim());
            }
            if (hrStaffInfo.getGraduationDate() != null) {
                hrStaffInfo.setGraduationDate(hrStaffInfo.getGraduationDate().trim().equals("") ? "0000-00-00" : hrStaffInfo.getGraduationDate().trim().trim());
            }
            if (hrStaffInfo.getBeginSalsryTime() != null) {
                hrStaffInfo.setBeginSalsryTime(hrStaffInfo.getBeginSalsryTime().trim().equals("") ? "0000-00-00" : hrStaffInfo.getBeginSalsryTime().trim().trim());
            }
            if (hrStaffInfo.getStaffCs() != null) {
                hrStaffInfo.setStaffCs(hrStaffInfo.getStaffCs().trim().equals("") ? "0000-00-00" : hrStaffInfo.getStaffCs().trim().trim());
            }
            if (hrStaffInfo.getStaffCtr() != null) {
                hrStaffInfo.setStaffCtr(hrStaffInfo.getStaffCtr().trim().equals("") ? "0000-00-00" : hrStaffInfo.getStaffCtr().trim().trim());
            }
            if (hrStaffInfo.getDatesEmployed() != null) {
                hrStaffInfo.setDatesEmployed(hrStaffInfo.getDatesEmployed().trim().equals("") ? "0000-00-00" : hrStaffInfo.getDatesEmployed().trim().trim());
            }
            if (hrStaffInfo.getLastUpdateTime() != null) {
                hrStaffInfo.setLastUpdateTime(hrStaffInfo.getLastUpdateTime().trim().equals("") ? "2000-01-01 00:00:00" : hrStaffInfo.getLastUpdateTime().trim().trim());
            }
            if (hrStaffInfo.getAddTime() != null) {
                hrStaffInfo.setAddTime(hrStaffInfo.getAddTime().trim().equals("") ? "2000-01-01 00:00:00" : hrStaffInfo.getAddTime().trim().trim());
            }
            if (hrStaffInfo.getRecordDate() != null) {
                hrStaffInfo.setRecordDate(hrStaffInfo.getRecordDate().trim().equals("") ? "0000-00-00" : hrStaffInfo.getRecordDate().trim().trim());
            }
            int count = 0;
            hrStaffInfo.setLastUpdateTime(DateFormat.getDatestr(new Date()));
            //如果该用户已有人事档案，则进行修改档案信息，并更新用户表
            if (hrStaffInfoMapper.getStaffByUserId(hrStaffInfo.getUserId()) != null) {
                HrStaffInfo hrStaffInfoTemp = hrStaffInfoMapper.getStaffByUserId(hrStaffInfo.getUserId());
                hrStaffInfo.setStaffId(hrStaffInfoTemp.getStaffId());
                if (!StringUtils.checkNull(hrStaffInfo.getPhotoName())) {
                 this.saveImg(request,hrStaffInfo);
                }
                hrStaffInfo.setWorkStatus("01");
                count += hrStaffInfoMapper.updatePersonFile(hrStaffInfo);
            } else {//若该用户没有人事档案，则进行创建人事档案，并将用户表进行更新
                Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
                Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
                hrStaffInfo.setCreateUserId(user.getUserId());//创建人
                hrStaffInfo.setCreateDeptId(user.getDeptId());//创建人所在部门
                hrStaffInfo.setWorkStatus("01");
                if (usersMapper.findUsersByuserId(hrStaffInfo.getUserId()) != null) {//当该用户还存在，并且在职
                    hrStaffInfo.setUserId(usersMapper.findUsersByuserId(hrStaffInfo.getUserId()).getUserId());
                } else {//该用户已离职
                    Users usersByuserId = usersMapper.findUsersByuserId(hrStaffInfo.getUserId());
                    if (usersByuserId != null) {
                        hrStaffInfo.setUserId(usersByuserId.getUserId());
                        hrStaffInfo.setWorkStatus("02");
                    }

                }
                hrStaffInfo.setAddTime(DateFormat.getDatestr(new Date()));
                if (!StringUtils.checkNull(hrStaffInfo.getPhotoName())) {
                    this.saveImg(request,hrStaffInfo);
                }
                if (hrStaffInfo.getStars() == null) {
                    hrStaffInfo.setStars("");
                }
                if (hrStaffInfo.getHonour() == null) {
                    hrStaffInfo.setHonour("");
                }
                count += hrStaffInfoMapper.insert(hrStaffInfo);
            }
            Users users = new Users();
            users.setUid(hrStaffInfo.getUid());
            users.setUserId(hrStaffInfo.getUserId());
            users.setUserName(hrStaffInfo.getUserName());
            if (hrStaffInfo.getWorkStatus().equals("01")) {
                users.setDeptId(hrStaffInfo.getDeptId());
            }
            users.setUserPriv(Integer.valueOf(hrStaffInfo.getUserPriv()));
            users.setSex(hrStaffInfo.getStaffSex());
            if (!StringUtils.checkNull(hrStaffInfo.getStaffBirth()) && !hrStaffInfo.getStaffBirth().equals("2000-01-01 00:00:00") && !hrStaffInfo.getStaffBirth().equals("0000-00-00")) {
                users.setBirthday(DateFormat.getDates(hrStaffInfo.getStaffBirth()));
            }
            users.setBloodType(hrStaffInfo.getBloodType());
            users.setIsLunar(hrStaffInfo.getIsLunar());
            users.setAddHome(hrStaffInfo.getHomeAddress());
            users.setTelNoHome(hrStaffInfo.getStaffPhone());
            users.setMobilNo(hrStaffInfo.getStaffMobile());
            users.setEmail(hrStaffInfo.getStaffEmail());
            users.setOicqNo(hrStaffInfo.getStaffQq());
            users.setMsn(hrStaffInfo.getStaffMsn());
            users.setPhoto(hrStaffInfo.getPhoto());
            users.setPostId(Integer.valueOf(hrStaffInfo.getJobPosition()));
            users.setJobId(Integer.valueOf(hrStaffInfo.getWorkJob()));
            users.setIdNumber(hrStaffInfo.getStaffCardNo());

            count += usersMapper.editUser(users);
            if (count > 1) {
                toJson.setMsg("ok");
                toJson.setFlag(0);
                Cookie redisSessionId = CookiesUtil.getCookieByName(request,"redisSessionId");
                Users u = SessionUtils.getSessionInfo(request.getSession(),Users.class,new Users(),redisSessionId);
                Syslog syslog = new Syslog();
                syslog.setUserId(u.getUserId());
                syslog.setType(7);
                String departmentName = usersMapper.getByUid(users.getUid()).getDeptName();
                syslog.setRemark("[" + departmentName + "]" + users.getUserName() + ",USER_ID=" + users.getUserId()+
                        ",人事档案模块操作");
                sysLogService.saveLog(syslog,request);


            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }


    public void saveImg(HttpServletRequest request,HrStaffInfo hrStaffInfo){
        Users temp = usersMapper.findUsersByuserId(hrStaffInfo.getUserId());
        String oldName = hrStaffInfo.getPhotoName().substring(0,hrStaffInfo.getPhotoName().indexOf(".") );
        if (oldName.equals(temp.getUid().toString())){
            return;
        }
        String realPath = request.getSession().getServletContext().getRealPath("/");
        String resourcePath = "ui/img/hr";
        File dir = new File(realPath + resourcePath);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        File realfile = new File(dir, hrStaffInfo.getPhotoName());
        String type = hrStaffInfo.getPhotoName().substring(hrStaffInfo.getPhotoName().indexOf(".") + 1);
        //Users temp = usersMapper.findUserByName(hrStaffInfo.getUserId());
        File oldTemp = new File(realPath + resourcePath + "/" + temp.getUid() + "." + type);
        if (oldTemp.exists()) {
            oldTemp.delete();
        }
        realfile.renameTo(new File(dir, temp.getUid() + "." + type));
        hrStaffInfo.setPhotoName(temp.getUid() + "." + type);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ToJson<Users> savePersonFile(HrStaffInfo hrStaffInfo, HttpServletRequest request) {
        ToJson<Users> toJson = new ToJson<Users>(1, "error");
        //添加用户表时，必须添加的字段：部门id、角色id、用户名（byname）、姓名（username）
        try {
            if (hrStaffInfo.getStaffBirth() != null) {
                hrStaffInfo.setStaffBirth(hrStaffInfo.getStaffBirth().trim().equals("") ? "0000-00-00" : hrStaffInfo.getStaffBirth().trim().trim());
            }
            if (hrStaffInfo.getJoinPartyTime() != null) {
                hrStaffInfo.setJoinPartyTime(hrStaffInfo.getJoinPartyTime().trim().equals("") ? "0000-00-00" : hrStaffInfo.getJoinPartyTime().trim().trim());
            }
            if (hrStaffInfo.getJobBeginning() != null) {
                hrStaffInfo.setJobBeginning(hrStaffInfo.getJobBeginning().trim().equals("") ? "0000-00-00" : hrStaffInfo.getJobBeginning().trim().trim());
            }
            if (hrStaffInfo.getGraduationDate() != null) {
                hrStaffInfo.setGraduationDate(hrStaffInfo.getGraduationDate().trim().equals("") ? "0000-00-00" : hrStaffInfo.getGraduationDate().trim().trim());
            }
            if (hrStaffInfo.getBeginSalsryTime() != null) {
                hrStaffInfo.setBeginSalsryTime(hrStaffInfo.getBeginSalsryTime().trim().equals("") ? "0000-00-00" : hrStaffInfo.getBeginSalsryTime().trim().trim());
            }
            if (hrStaffInfo.getStaffCs() != null) {
                hrStaffInfo.setStaffCs(hrStaffInfo.getStaffCs().trim().equals("") ? "0000-00-00" : hrStaffInfo.getStaffCs().trim().trim());
            }
            if (hrStaffInfo.getStaffCtr() != null) {
                hrStaffInfo.setStaffCtr(hrStaffInfo.getStaffCtr().trim().equals("") ? "0000-00-00" : hrStaffInfo.getStaffCtr().trim().trim());
            }
            if (hrStaffInfo.getDatesEmployed() != null) {
                hrStaffInfo.setDatesEmployed(hrStaffInfo.getDatesEmployed().trim().equals("") ? "0000-00-00" : hrStaffInfo.getDatesEmployed().trim().trim());
            }
            if (hrStaffInfo.getLastUpdateTime() != null) {
                hrStaffInfo.setLastUpdateTime(hrStaffInfo.getLastUpdateTime().trim().equals("") ? "2000-01-01 00:00:00" : hrStaffInfo.getLastUpdateTime().trim().trim());
            }
            if (hrStaffInfo.getAddTime() != null) {
                hrStaffInfo.setAddTime(hrStaffInfo.getAddTime().trim().equals("") ? "2000-01-01 00:00:00" : hrStaffInfo.getAddTime().trim().trim());
            }
            if (hrStaffInfo.getRecordDate() != null) {
                hrStaffInfo.setRecordDate(hrStaffInfo.getRecordDate().trim().equals("") ? "0000-00-00" : hrStaffInfo.getRecordDate().trim().trim());
            }
            //插入人事档案信息
            Users users = new Users();
            users.setPostId(0);
            users.setDeptId(hrStaffInfo.getDeptId());
            if (!StringUtils.checkNull(hrStaffInfo.getUserPriv())) {
                users.setUserPriv(Integer.valueOf(hrStaffInfo.getUserPriv()));
            }
            if(hrStaffInfo.getUserId() != null && !"".equals(hrStaffInfo.getUserId().trim())){
                users.setByname(hrStaffInfo.getUserId());
                if(hrStaffInfo.getStaffName() == null  || "".equals(hrStaffInfo.getStaffName().trim())){
                    users.setUserName(hrStaffInfo.getUserId());
                }else{
                    users.setUserName(hrStaffInfo.getStaffName());
                }
            }else{
                toJson.setMsg("未填写用户名");
                toJson.setFlag(1);
                return toJson;
            }

            users.setPhoto(hrStaffInfo.getPhoto());
            users.setTheme((byte) 6);
            users.setPassword("");
            if (!StringUtils.checkNull(hrStaffInfo.getJobPosition())) {
                users.setPostId(Integer.valueOf(hrStaffInfo.getJobPosition()));
            }
            if (!StringUtils.checkNull(hrStaffInfo.getWorkJob())) {
                users.setJobId(Integer.valueOf(hrStaffInfo.getWorkJob()));
            }
            users.setNotLogin((byte) 0);
            users.setNotMobileLogin(0);
            UserExt userExt = new UserExt();
            ModulePriv modulePriv = new ModulePriv();
            toJson = usersService.addUser(users, userExt, modulePriv, request);
            //先判断是否可以正常创建用户，可以则进行人事档案的创建
            if (toJson.isFlag()) {
                Users temp = (Users) toJson.getObject();
                Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
                Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
                hrStaffInfo.setCreateUserId(user.getUserId());//创建人
                hrStaffInfo.setCreateDeptId(user.getDeptId());//创建人所在部门
                hrStaffInfo.setWorkStatus("01");
                hrStaffInfo.setUserId(temp.getUserId());
                hrStaffInfo.setAddTime(DateFormat.getDatestr(new Date()));

                if (!StringUtils.checkNull(hrStaffInfo.getPhotoName())) {
                    String realPath = request.getSession().getServletContext().getRealPath("/");
                    String resourcePath = "ui/img/hr";
                    File dir = new File(realPath + resourcePath);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    File realfile = new File(dir, hrStaffInfo.getPhotoName());
                    String type = hrStaffInfo.getPhotoName().substring(hrStaffInfo.getPhotoName().indexOf(".") + 1);
                    //Users temp = usersMapper.findUserByName(hrStaffInfo.getUserId());
                    realfile.renameTo(new File(dir, temp.getUid() + "." + type));
                    hrStaffInfo.setPhotoName(temp.getUid() + "." + type);
                }
                if (hrStaffInfo.getStars() == null) {
                    hrStaffInfo.setStars("");
                }
                if (hrStaffInfo.getHonour() == null) {
                    hrStaffInfo.setHonour("");
                }
                int count = hrStaffInfoMapper.insert(hrStaffInfo);
                if (count > 0) {
                    toJson.setMsg("ok");
                    toJson.setFlag(0);

                    UserDeptOrder userDeptOrders = new UserDeptOrder();//新建人员时插入
                    userDeptOrders.setDeptId(hrStaffInfo.getDeptId());
                    userDeptOrders.setUserId(hrStaffInfo.getUserId());
                    userDeptOrders.setOrderNo(users.getUserNo());
                    userDeptOrders.setPostId(users.getPostId());
                    UserDeptOrder userDeptOrder1 = userDeptOrderMapper.selectUserAndDept(hrStaffInfo.getUserId(), hrStaffInfo.getDeptId());
                    if (userDeptOrder1==null){// 防止部门 和辅助部门设置同一个
                        userDeptOrderMapper.insertSelective(userDeptOrders);
                    }
                }



            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }


    @Override
    public ToJson<HrStaffInfo> deletePersonById(String staffIds) {
        ToJson<HrStaffInfo> toJson = new ToJson<HrStaffInfo>(1, "error");
        try {
            int count = 0;
            String[] staffIdArray = staffIds.split(",");
            for (String staffId : staffIdArray) {
                if (!StringUtils.checkNull(staffId)) {
                    HrStaffInfo staffInfo = hrStaffInfoMapper.getStaffByStaffId(staffId);
                    if (!StringUtils.checkNull(staffInfo.getWorkStatus()) && staffInfo.getWorkStatus().equals("02")) {
                        //如果该人员已离职，也应将其从离职表中删除
                        count += hrStaffLeaveMapper.deleteByLeavePerson(staffInfo.getUserId());//删除离职表中的数据
                    }
                    count += hrStaffInfoMapper.deletePersonById(staffId);//删除人事档案表中的数据
                    String uids[] = {staffInfo.getUserId()};
                    //删除用户表中的人
                    usersMapper.deleteUser(uids);

                }
            }
            if (count > 0) {
                toJson.setFlag(0);
                toJson.setMsg("ok");
            }
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            L.e("HrRetireeServiceImpl deletePersonById:" + e);
        }
        return toJson;
    }


    @Override
    public ToJson<Object> selectStaffInfoById(String staffId) {
        ToJson<Object> json = new ToJson<Object>();
        try {
            if (staffId != null) {
                HrStaffInfo hrStaffInfo = hrStaffInfoMapper.selectStaffInfoById(Integer.valueOf(staffId));
                /* hrStaffInfo.setDeptName(departmentMapper.getDeptNameByDeptId(hrStaffInfo.getDeptId()));*/
                /*  if(hrStaffInfo.getDeptId()!=null){
                       //根据部门id查询部门名称
                      String deptNameByDeptId = departmentMapper.getDeptNameByDeptId(hrStaffInfo.getDeptId());
                  }*/
                //处理职务
                if (hrStaffInfo.getJobPosition() == null || "null".equals(hrStaffInfo.getJobPosition())) {
                    hrStaffInfo.setJobPositionStr("");
                } else {
                    UserPost userPost = new UserPost();
                    List<UserPost> list = dutiesManagementMapper.selectUserPostList(userPost);
                    for (UserPost userPost1 : list) {
                        if (hrStaffInfo.getJobPosition().equals(String.valueOf(userPost1.getPostId()))) {
                            hrStaffInfo.setJobPositionStr(userPost1.getPostName());
                        }
                    }
                }
                //岗位
                if (hrStaffInfo.getWorkJob() == null || "null".equals(hrStaffInfo.getJobPosition())) {
                    hrStaffInfo.setWorkJobStr("");
                } else {
                    UserJob userJob = new UserJob();
                    List<UserJob> list = positionManagementMapper.selectUserJobList(userJob);
                    for (UserJob userJob1 : list) {
                        if (hrStaffInfo.getWorkJob().equals(String.valueOf(userJob1.getJobId()))) {
                            hrStaffInfo.setWorkJobStr(userJob1.getJobName());
                        }
                    }
                }

                hrStaffInfo.setPresentPositionName(hrStaffInfo.getPresentPosition() != null &&hrCodeMapper.selectCode("PRESENT_POSITION",hrStaffInfo.getPresentPosition())!=null ?hrCodeMapper.selectCode("PRESENT_POSITION",hrStaffInfo.getPresentPosition()).getCodeName() : "");

                hrStaffInfo.setStaffOccupationName(hrStaffInfo.getStaffOccupation() != null && hrCodeMapper.selectCode("STAFF_OCCUPATION",hrStaffInfo.getStaffOccupation())!=null?hrCodeMapper.selectCode("STAFF_OCCUPATION",hrStaffInfo.getStaffOccupation()).getCodeName() : "");
                if (hrStaffInfo.getProjectName() != null) {
                    Integer pid = hrStaffInfo.getProjectName();
                    String pname = sysCodeMapper.getCodeName(pid);
                    hrStaffInfo.setProjectNAME(pname);
                }
                if (hrStaffInfo.getLocationAddress() != null) {
                    Integer lid = hrStaffInfo.getLocationAddress();
                    String lname = sysCodeMapper.getCodeName(lid);
                    hrStaffInfo.setLocationAddressName(lname);
                }

                json.setFlag(0);
                json.setMsg("ok");
                json.setObject(hrStaffInfo);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
            json.setObject(null);
        }
        return json;
    }

    /**
     * 创建作者:   牛江丽
     * 创建日期:   2017年7月25日 下午15:30:00
     * 方法介绍:   查询未建档人人数
     * 参数说明:   @param deptId部门编号
     * 参数说明:   @return
     *
     * @return int人数
     */
    @Override
    public ToJson<HrStaffInfo> queryCountNoDocument(String deptId) {
        ToJson<HrStaffInfo> json = new ToJson<HrStaffInfo>(1, "error");
        try {
            int count = hrStaffInfoMapper.queryCountNoDocument(deptId);
            json.setTotleNum(count);
            json.setFlag(0);
            json.setMsg("ok");
        } catch (Exception e) {
            json.setMsg(e.getMessage());
            L.e("HrRetireeServiceImpl queryNoDocument:" + e);
        }
        return json;
    }

    @Override
    public ToJson<HrStaffInfo> getUserByDeptId(Integer deptId) {
        ToJson<HrStaffInfo> toJson = new ToJson<HrStaffInfo>(1, "error");
        try {
            List<HrStaffInfo> list = hrStaffInfoMapper.getUserByDeptId(deptId);
            if (list.size() > 0) {
                toJson.setObj(list);
                toJson.setMsg("ok");
            } else {
                toJson.setMsg("没有用户");
            }
            toJson.setFlag(0);
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    /**
     * 创建作者：牛江丽
     * 方法介绍：上传人事档案中的头像,并修改头像
     *
     * @param request
     * @param hrStaffInfo
     * @return
     */
    @Override
    public ToJson<HrStaffInfo> uploadImage(HttpServletRequest request, MultipartFile imageFile, HrStaffInfo hrStaffInfo) throws IllegalStateException, IOException {
        ToJson<HrStaffInfo> toJson = new ToJson<HrStaffInfo>(1, "error");
        try {
            String realPath = request.getSession().getServletContext().getRealPath("/");
            String resourcePath = "ui/img/hr";

            //先查询是否已经上传了该头像，如果上传了删除后在上传，否则直接上传
            Users users = usersMapper.findUsersByuserId(hrStaffInfo.getUserId());
            HrStaffInfo hrStaffInfoTemp = new HrStaffInfo();
            if (users != null) {
                hrStaffInfoTemp = hrStaffInfoMapper.getStaffByUserId(users.getUserId());
            } else {
                hrStaffInfoTemp = hrStaffInfoMapper.getStaffByUserId(hrStaffInfo.getUserId());
            }
            if (hrStaffInfoTemp == null) {
                Users users1 = usersMapper.findUserByName(hrStaffInfo.getUserId());
                if (users1 != null) {
                    hrStaffInfoTemp = hrStaffInfoMapper.getStaffByUserId(users1.getUserId());
                } else {
                    hrStaffInfoTemp = hrStaffInfoMapper.getStaffByUserId(hrStaffInfo.getUserId());
                }
            }
            if (!StringUtils.checkNull(hrStaffInfoTemp.getPhotoName())) {
                File temp = new File(realPath + resourcePath + hrStaffInfoTemp.getPhotoName());
                if (temp.exists()) {
                    temp.delete();
                }
            }
            //上传图片并进行修改数据库数据
            if (imageFile != null) {
                if (FileUploadUtil.allowUpload(imageFile.getContentType())) {
                    String fileName = FileUploadUtil.rename(imageFile.getOriginalFilename());
                    File dir = new File(realPath + resourcePath);
                    if (!dir.exists()) {
                        dir.mkdirs();
                    }
                    File file = new File(dir, fileName);
                    imageFile.transferTo(file);
                    hrStaffInfo.setPhotoName(fileName);
                }
                hrStaffInfo.setUserId("");
                hrStaffInfo.setStaffId(hrStaffInfoTemp.getStaffId());
                int count = hrStaffInfoMapper.updatePersonFile(hrStaffInfo);
                if (count > 0) {
                    toJson.setFlag(0);
                    toJson.setMsg("ok");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    /**
     * @return
     * @创建作者： 杨超
     * @方法介绍：返回所有职员的工作年限信息
     */
    public BaseWrapper getAllWorkYearsInfo(int space) {
        BaseWrapper baseWrapper = new BaseWrapper();
        List<HrStaffInfo> hrStaffInfos = hrStaffInfoMapper.getAllWorkYearsInfo();
        HashMap<String, Integer> temp = new HashMap<>();
        for (HrStaffInfo hs : hrStaffInfos) {
            //如果人事档案中存在员工信息
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            int age = -1;
            if (hs != null) {
                //人事档案中用户工作年龄不为空
                if (hs.getWorkAge() != null && !"".equals(hs.getWorkAge())) {
                    age = Integer.parseInt(hs.getWorkAge());
                    age = age / space;
                } else {
                    //人事档案中用户总工龄为空，通过入职时间判断
                    Date d = null;
                    try {
                        d = simpleDateFormat.parse(hs.getDatesEmployed());
                    } catch (ParseException e) {
                        L.e("工作时间转换出错");
                        L.e(e);
                    }
                    if (d != null) {
                        long years = (date.getTime() - d.getTime()) / (1000 * 60 * 60 * 24) / 365;
                        age = (int) (years) / space;
                    } else {
                        age = -1;
                    }
                }
            } else {
                age = -1;
            }

            if (age >= 0 && age <= (65 / space)) {
                if (temp.containsKey("years" + age)) {
                    temp.put("years" + age, temp.get("years" + age) + 1);
                } else {
                    temp.put("years" + age, 1);
                }
            } else {
                if (temp.containsKey("Unknown")) {
                    temp.put("Unknown", temp.get("Unknown") + 1);
                } else {
                    temp.put("Unknown", 1);
                }
            }
        }
        baseWrapper.setData(temp);
        baseWrapper.setStatus(true);
        baseWrapper.setFlag(true);
        return baseWrapper;
    }

    /**
     * @return
     * @创建作者： 杨超
     * @方法介绍：返回所有职员的学历信息
     */
    public BaseWrapper getAllRecordInfo() {
        BaseWrapper baseWrapper = new BaseWrapper();
        List<HrStaffInfo> hrStaffInfos = hrStaffInfoMapper.getAllWorkYearsInfo();
        HashMap<String, Integer> temp = new HashMap<>();
        for (HrStaffInfo hs : hrStaffInfos) {
            String staffHighestSchool = "Unknown";
            if (hs != null) {
                staffHighestSchool = hs.getStaffHighestSchool();
                if (staffHighestSchool != null && !"".equals(staffHighestSchool)) {

                } else {
                    staffHighestSchool = "Unknown";
                }
            }
            if (temp.containsKey(staffHighestSchool)) {
                temp.put(staffHighestSchool, temp.get(staffHighestSchool) + 1);
            } else {
                temp.put(staffHighestSchool, 1);
            }
        }
        baseWrapper.setData(temp);
        baseWrapper.setStatus(true);
        baseWrapper.setFlag(true);
        return baseWrapper;
    }

    @Override
    public ToJson<HrStaffInfo> importStaffInfo(MultipartFile file, HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        ToJson<HrStaffInfo> json = new ToJson<HrStaffInfo>();
        // 成功次数
        Integer successCount = 0;
        try {
            //判读当前系统
            //读取配置文件
            ResourceBundle rb = ResourceBundle.getBundle("upload");
            String osName = System.getProperty("os.name");
            StringBuffer path = new StringBuffer();
            StringBuffer buffer = new StringBuffer();
            if (osName.toLowerCase().startsWith("win")) {
                //sb=sb.append(rb.getString("upload.win"));
                //判断路径是否是相对路径，如果是的话，加上运行的路径
                String uploadPath = rb.getString("upload.win");
                if (uploadPath.indexOf(":") == -1) {
                    //获取运行时的路径
                    String basePath = this.getClass().getClassLoader().getResource(".").getPath() + File.pathSeparator;
                    //获取截取后的路径
                    String str2 = "";
                    if (basePath.indexOf("/xoa") != -1) {
                        //获取字符串的长度
                        int length = basePath.length();
                        //返回指定字符在此字符串中第一次出现处的索引
                        int index = basePath.indexOf("/xoa");
                        //从指定位置开始到指定位置结束截取字符串
                        str2 = basePath.substring(0, index);
                    }
                    path = path.append(str2 + "/xoa");
                }
                path.append(uploadPath);
                buffer = buffer.append(rb.getString("upload.win"));
            } else {
                path = path.append(rb.getString("upload.linux"));
                buffer = buffer.append(rb.getString("upload.linux"));
            }

            // 判断是否为空文件
            if (file.isEmpty()) {
                json.setMsg("请上传文件！");
                json.setFlag(1);
                return json;
            } else {
                String fileName = file.getOriginalFilename();
                if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {
                    String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                    int pos = fileName.indexOf(".");
                    String extName = fileName.substring(pos);
                    String newFileName = uuid + extName;
                    File filedir = new File(path.toString());
                    if (!filedir.exists()) {
                        filedir.mkdirs();
                    }
                    File dest = new File(path.toString(), newFileName);
                    file.transferTo(dest);
                    // 将文件上传成功后进行读取文件
                    // 进行读取的路径
                    String readPath = path.append(System.getProperty("file.separator") + newFileName).toString();
                    InputStream in = new FileInputStream(readPath);
                    HSSFWorkbook excelObj = new HSSFWorkbook(in);
                    HSSFSheet sheetObj = excelObj.getSheetAt(0);
                    Row row = sheetObj.getRow(0);
                    int colNum = row.getPhysicalNumberOfCells();
                    int lastRowNum = sheetObj.getLastRowNum();
                    HrStaffInfoWithBLOBs hrStaffInfo = null;

                    for (int i = 1; i <= lastRowNum; i++) {
                        hrStaffInfo = new HrStaffInfoWithBLOBs();
                        row = sheetObj.getRow(i);
                        if (row != null) {
                            if(row.getCell(0) == null || StringUtils.checkNull(row.getCell(0).toString())){
                                continue ;
                            }
                            for (int j = 0; j < colNum; j++) {
                                Cell cell = row.getCell(j);
                                if (cell != null) {
                                        if (getCellValue(cell)!=null){
                                            switch (j) {
                                                case 0:
                                                    //用户名
                                                    hrStaffInfo.setUserId(getCellValue(cell));
                                                    break;
                                                case 1:
                                                    //部门
                                                    List<String> parentList = departmentMapper.getDeptIdByDeptName(getCellValue(cell));
                                                    String deptIdByDeptName = "";
                                                    if (parentList.size() > 0) {
                                                        deptIdByDeptName = parentList.get(0);
                                                    }
                                                    if (!StringUtils.checkNull(deptIdByDeptName)) {
                                                        hrStaffInfo.setDeptId(Integer.valueOf(deptIdByDeptName));
                                                    }
                                                    break;
                                                case 2:
                                                    //员工编号
                                                    hrStaffInfo.setStaffNo(String.valueOf(getCellValue(cell)));
                                                    break;
                                                case 3:
                                                    //员工工号
                                                    hrStaffInfo.setWorkNo(String.valueOf(getCellValue(cell)));
                                                    break;
                                                case 4:
                                                    //员工姓名
                                                    hrStaffInfo.setStaffName(getCellValue(cell));
                                                    break;
                                                case 5:
                                                    // 身份证号
                                                    hrStaffInfo.setStaffCardNo(getCellValue(cell));
                                                    break;
                                                case 6:
                                                    //性别
                                                    hrStaffInfo.setStaffSex(String.valueOf(getCellValue(cell)));
                                                    break;
                                                case 7:
                                                    //民族
                                                    hrStaffInfo.setStaffNationality(getCellValue(cell));
                                                    break;
                                                case 8:
                                                    // 年龄
                                                    hrStaffInfo.setStaffAge(String.valueOf(getCellValue(cell)));
                                                    break;
                                                case 9:
                                                    // 籍贯
                                                    hrStaffInfo.setStaffNativePlace(getCellValue(cell));
                                                    hrStaffInfo.setStaffNativePlace2(getCellValue(cell));
                                                    break;
                                                case 10:
                                                    String stringCellValue = getCellValue(cell);
                                                    if (stringCellValue.equals("群众")) {
                                                        hrStaffInfo.setStaffPoliticalStatus("1");
                                                    } else if (stringCellValue.equals("共青团员")) {
                                                        hrStaffInfo.setStaffPoliticalStatus("2");
                                                    } else if (stringCellValue.equals("中共党员")) {
                                                        hrStaffInfo.setStaffPoliticalStatus("3");
                                                    } else if (stringCellValue.equals("中共预备党员")) {
                                                        hrStaffInfo.setStaffPoliticalStatus("4");
                                                    } else if (stringCellValue.equals("民主党派")) {
                                                        hrStaffInfo.setStaffPoliticalStatus("5");
                                                    } else {
                                                        hrStaffInfo.setStaffPoliticalStatus("6");
                                                    }
                                                    break;
                                                case 11:
                                                    // 手机号码
                                                    hrStaffInfo.setStaffMobile(String.valueOf(getCellValue(cell)));
                                                    break;
                                                case 12:
                                                    // 邮箱地址
                                                    hrStaffInfo.setStaffEmail(getCellValue(cell));
                                                    break;
                                                case 13:
                                                    // QQ
                                                    hrStaffInfo.setStaffQq(String.valueOf(getCellValue(cell)));
                                                    break;
                                                case 14:
                                                    // 家庭地址
                                                    hrStaffInfo.setHomeAddress(getCellValue(cell));
                                                    break;
                                                case 15:
                                                    // 健康状态
                                                    hrStaffInfo.setStaffHealth(getCellValue(cell));
                                                    break;
                                                case 16:
                                                    // OA角色
                                                    UserPriv userPrivByName = userPrivMapper.getUserPrivByName(getCellValue(cell));
                                                    if (userPrivByName != null) {
                                                        hrStaffInfo.setUserPriv(userPrivByName.getUserPriv().toString());
                                                    } else {
                                                        json.setMsg("用户角色不存在");
                                                        dest.delete();
                                                        return json;
                                                    }
                                                    break;
                                                case 17:
                                                    //项目名称
                                                    if(getCellValue(cell)!= null && !"".equals(getCellValue(cell).trim())){
                                                        Integer projectName = sysCodeMapper.getCodeId(getCellValue(cell));
                                                        if (projectName != null) {
                                                            hrStaffInfo.setProjectName(projectName);
                                                        }else{
                                                            hrStaffInfo.setProjectName(null);
                                                        }
                                                    }

                                                    break;
                                                case 18:
                                                    //所在位置
                                                    if(getCellValue(cell)!=null && !"".equals(getCellValue(cell))){
                                                        Integer locationAddress = sysCodeMapper.getCodeId(getCellValue(cell));
                                                        if (locationAddress != null) {
                                                            hrStaffInfo.setLocationAddress(locationAddress);
                                                        } else {
                                                            hrStaffInfo.setLocationAddress(null);
                                                        }
                                                    }
                                                    break;
                                            }
                                        }
                                }
                            }
                            String userId = hrStaffInfo.getUserId();
                            Users users = usersMapper.getUsersBybyname(userId);
                            if (users != null) {
                                String staffInfoUserId = users.getUserId();
                                HrStaffInfo singleHrStaffInfo = hrStaffInfoMapper.getStaffByUserId(staffInfoUserId);

                                if (singleHrStaffInfo != null) {
                                    hrStaffInfo.setStaffId(singleHrStaffInfo.getStaffId());
                                    hrStaffInfo.setDeptId(users.getDeptId());
                                    int i1 = hrStaffInfoMapper.updateByPrimaryKeySelective(hrStaffInfo);
                                    if (i1 > 0) {
                                        successCount++;

                                        // 说明要更新user_dept_order   先判断移入到的部门是否重复
                                        UserDeptOrder userDeptOrder1 = userDeptOrderMapper.selectUserAndDept(hrStaffInfo.getUserId(), hrStaffInfo.getDeptId());
                                        if (userDeptOrder1==null){   // 防止部门 和辅助部门设置同一个        查询出主部门  更新
                                            UserDeptOrder userDeptOrder2 = userDeptOrderMapper.selectUserAndDept(users.getUserId(), users.getDeptId());
                                            if(userDeptOrder2 != null){
                                                userDeptOrder2.setDeptId(hrStaffInfo.getDeptId());
                                                userDeptOrderMapper.updateByPrimaryKeySelective(userDeptOrder2);
                                            }
                                        }
                                    }
                                } else {
                                    hrStaffInfo.setDeptId(users.getDeptId());
                                    if (hrStaffInfo.getStaffBirth() != null) {
                                        hrStaffInfo.setStaffBirth(hrStaffInfo.getStaffBirth().trim().equals("") ? "0000-00-00" : hrStaffInfo.getStaffBirth().trim().trim());
                                    }
                                    if (hrStaffInfo.getJoinPartyTime() != null) {
                                        hrStaffInfo.setJoinPartyTime(hrStaffInfo.getJoinPartyTime().trim().equals("") ? "0000-00-00" : hrStaffInfo.getJoinPartyTime().trim().trim());
                                    }
                                    if (hrStaffInfo.getJobBeginning() != null) {
                                        hrStaffInfo.setJobBeginning(hrStaffInfo.getJobBeginning().trim().equals("") ? "0000-00-00" : hrStaffInfo.getJobBeginning().trim().trim());
                                    }
                                    if (hrStaffInfo.getGraduationDate() != null) {
                                        hrStaffInfo.setGraduationDate(hrStaffInfo.getGraduationDate().trim().equals("") ? "0000-00-00" : hrStaffInfo.getGraduationDate().trim().trim());
                                    }
                                    if (hrStaffInfo.getBeginSalsryTime() != null) {
                                        hrStaffInfo.setBeginSalsryTime(hrStaffInfo.getBeginSalsryTime().trim().equals("") ? "0000-00-00" : hrStaffInfo.getBeginSalsryTime().trim().trim());
                                    }
                                    if (hrStaffInfo.getStaffCs() != null) {
                                        hrStaffInfo.setStaffCs(hrStaffInfo.getStaffCs().trim().equals("") ? "0000-00-00" : hrStaffInfo.getStaffCs().trim().trim());
                                    }
                                    if (hrStaffInfo.getStaffCtr() != null) {
                                        hrStaffInfo.setStaffCtr(hrStaffInfo.getStaffCtr().trim().equals("") ? "0000-00-00" : hrStaffInfo.getStaffCtr().trim().trim());
                                    }
                                    if (hrStaffInfo.getDatesEmployed() != null) {
                                        hrStaffInfo.setDatesEmployed(hrStaffInfo.getDatesEmployed().trim().equals("") ? "0000-00-00" : hrStaffInfo.getDatesEmployed().trim().trim());
                                    }
                                    if (hrStaffInfo.getLastUpdateTime() != null) {
                                        hrStaffInfo.setLastUpdateTime(hrStaffInfo.getLastUpdateTime().trim().equals("") ? "2000-01-01 00:00:00" : hrStaffInfo.getLastUpdateTime().trim().trim());
                                    }
                                    if (hrStaffInfo.getAddTime() != null) {
                                        hrStaffInfo.setAddTime(hrStaffInfo.getAddTime().trim().equals("") ? "0000-0bn 0-00 00:00:00" : hrStaffInfo.getAddTime().trim().trim());
                                    }
                                    if (hrStaffInfo.getRecordDate() != null) {
                                        hrStaffInfo.setRecordDate(hrStaffInfo.getRecordDate().trim().equals("") ? "0000-00-00" : hrStaffInfo.getRecordDate().trim().trim());
                                    }
                                    Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
                                    Users user = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
                                    hrStaffInfo.setCreateUserId(user.getUserId());//创建人
                                    hrStaffInfo.setCreateDeptId(user.getDeptId());//创建人所在部门
                                    hrStaffInfo.setWorkStatus("01");
                                    hrStaffInfo.setUserId(staffInfoUserId);
                                    hrStaffInfo.setAddTime(DateFormat.getDatestr(new Date()));
                                    if (hrStaffInfo.getStars() == null) {
                                        hrStaffInfo.setStars("");
                                    }
                                    if (hrStaffInfo.getHonour() == null) {
                                        hrStaffInfo.setHonour("");
                                    }
                                    int count = hrStaffInfoMapper.insert(hrStaffInfo);
                                    if (count > 0) {
                                        successCount++;
                                        
                                        // 说明要更新user_dept_order   先判断移入到的部门是否重复
                                        UserDeptOrder userDeptOrder1 = userDeptOrderMapper.selectUserAndDept(hrStaffInfo.getUserId(), hrStaffInfo.getDeptId());
                                        if (userDeptOrder1==null){   // 防止部门 和辅助部门设置同一个        查询出主部门  更新
                                            UserDeptOrder userDeptOrder2 = userDeptOrderMapper.selectUserAndDept(users.getUserId(), users.getDeptId());
                                            if(userDeptOrder2 != null){
                                                userDeptOrder2.setDeptId(hrStaffInfo.getDeptId());
                                                userDeptOrderMapper.updateByPrimaryKeySelective(userDeptOrder2);
                                            }
                                        }
                                    }
                                }
                            } else {
                                ToJson<Users> usersToJson = this.savePersonFile(hrStaffInfo, request);
                                if (usersToJson.isFlag()) {
                                    successCount++;
                                }
                            }


                        }
                    }
                    dest.delete();
                } else {
                    json.setMsg("file type error!");
                    json.setFlag(1);
                    return json;
                }
            }
            json.setTotleNum(successCount);
            json.setMsg("ok");
            json.setFlag(0);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg(e.getMessage());
            json.setFlag(1);
        }
        return json;
    }

    private String getCellValue(Cell cell) {

        Object value = null;
        switch (cell.getCellType()) {
            case STRING:
                value = cell.getStringCellValue();
                break;
            case NUMERIC:
                value = (int) cell.getNumericCellValue();
                break;
            default:
                value = cell.getStringCellValue();
                break;
        }
        return String.valueOf(value);
    }


    @Override
    public ToJson<Object> selectStaffInfoByIds(String userId) {
        ToJson<Object> json = new ToJson<Object>();
        try {
            if (userId != null) {
                HrStaffInfo hrStaffInfo = hrStaffInfoMapper.selectStaffInfoByIds(userId);
                json.setFlag(0);
                json.setMsg("ok");
                json.setObject(hrStaffInfo);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
            json.setObject(null);
        }
        return json;
    }

    @Override
    public ToJson<Object> getExperInformationList() {
        ToJson<Object> json = new ToJson<Object>();
        try {
            List<HrStaffInfo> hrStaffInfo = hrStaffInfoMapper.getExperInformationList();
            json.setFlag(0);
            json.setMsg("ok");
            json.setObject(hrStaffInfo);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
            json.setObject(null);
        }
        return json;
    }

    /**
     * 李阳
     * 在职 查询档案信息  分页
     */
    @Override
    public ToJson<HrStaffInfo> statusOne(HttpServletRequest request, HttpServletResponse response, HrStaffInfo hrStaffInfo, String deptIds, Integer page, Integer pageSize, boolean useFlag, String export) {
        ToJson<HrStaffInfo> json = new ToJson<HrStaffInfo>();
        PageParams pages = new PageParams();
        pages.setPage(page);
        pages.setPageSize(pageSize);
        pages.setUseFlag(useFlag);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("page", pages);
        if (deptIds != null) {
            if (!StringUtils.checkNull(deptIds.toString())) {
                String fromIds = deptIds.toString();
                String[] substring = fromIds.split(",");
                map.put("deptIds", substring);
            }
        }
        if (hrStaffInfo.getJobPosition() != null & !StringUtils.checkNull(hrStaffInfo.getJobPosition())) {
            if (hrStaffInfo.getJobPosition().equals("null")) {
                hrStaffInfo.setJobPosition(null);
            }
        }
        if (hrStaffInfo.getUserPriv() != null & !StringUtils.checkNull(hrStaffInfo.getUserPriv())) {
            if (hrStaffInfo.getUserPriv().equals("undefined")) {
                hrStaffInfo.setUserPriv(null);
            }
        }
        map.put("hrStaffInfo", hrStaffInfo);
        try {
            if (export == null) {
                export = "0";
            }
            List<HrStaffInfo> list = hrStaffInfoMapper.statusOne(map);
            if (list.size() > 0) {
                for (HrStaffInfo list1 : list) {
                    //政治面貌
                    HrCode hr_staff_contract1 = hrCodeMapper.selectCode("STAFF_POLITICAL_STATUS", list1.getStaffPoliticalStatus());
                    if (hr_staff_contract1 != null) {
                        list1.setStaffPoliticalStatusName(hr_staff_contract1.getCodeName());
                    } else {
                        list1.setStaffPoliticalStatusName(" ");
                    }
                    //籍贯
                    HrCode hr_staff_contract2 = hrCodeMapper.selectCode("AREA", list1.getStaffNativePlace());
                    if (hr_staff_contract2 != null) {
                        list1.setStaffNativePlaceName(hr_staff_contract2.getCodeName());
                    } else {
                        list1.setStaffNativePlaceName(" ");
                    }
                }
                json.setTotleNum(pages.getTotal());
                json.setObj(list);
                json.setMsg("ok");
                json.setFlag(0);
            } else {
                json.setMsg("档案信息为空");
            }
            if (export.equals("1")) {  //导出
                if (list != null) {
                    for (HrStaffInfo list1 : list) {
                        if ("0".equals(list1.getStaffMaritalStatus())) {
                            list1.setStatusName("未婚");
                        }
                        if ("1".equals(list1.getStaffMaritalStatus())) {
                            list1.setStatusName("已婚");
                        }
                        if ("2".equals(list1.getStaffMaritalStatus())) {
                            list1.setStatusName("离异");
                        }
                        if ("3".equals(list1.getStaffMaritalStatus())) {
                            list1.setStatusName("丧偶");
                        } else {
                            list1.setStatusName(" ");
                        }
                        if ("0".equals(list1.getStaffSex())) {
                            list1.setStaffSexName("男");
                        } else {
                            list1.setStaffSexName("女");
                        }
                        //学历
                        HrCode hr_staff_contract1 = hrCodeMapper.selectCode("STAFF_HIGHEST_SCHOOL", list1.getStaffHighestSchool());
                        if (hr_staff_contract1 != null) {
                            list1.setStaffHighestSchoolName(hr_staff_contract1.getCodeName());
                        } else {
                            list1.setStaffHighestSchoolName(" ");
                        }
                        //在职状态
                        HrCode hr_staff_contract = hrCodeMapper.selectCode("WORK_STATUS", list1.getWorkStatus());
                        if (hr_staff_contract != null) {
                            list1.setWorkStatusName(hr_staff_contract.getCodeName());
                        } else {
                            list1.setWorkStatusName(" ");
                        }
                    }
                    HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("人事档案", 14);
                    String[] secondTitles = {"部门", "姓名", "角色", "性别", "年龄", "出生日期", "手机号码", "学历", "毕业院校", "在职状态", "专业", "学位", "户口所在地", "婚姻状况"};
                    HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);
                    String[] beanProperty = {"deptName", "staffName", "userPrivName", "staffSexName", "staffAge", "staffBirth", "staffMobile", "staffHighestSchoolName", "graduationSchool", "workStatusName", "staffMajor", "staffHighestDegree", "staffDomicilePlace", "statusName"};
                    HSSFWorkbook workbook = null;
                    workbook = ExcelUtil.exportExcelData(workbook2, list, beanProperty);
                    response.setContentType("text/html;charser=UTF-8");
                    ServletOutputStream out = response.getOutputStream();
                    String filename = "人事档案.xls";//考虑多语言
                    filename = FileUtils.encodeDownloadFilename(filename,
                            request.getHeader("user-agent"));
                    response.setContentType("application/vnd.ms-excel");
                    response.setHeader("content-disposition",
                            "attachment;filename=" + filename);
                    workbook.write(out);
                    out.flush();
                    out.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }

    /**
     * 李阳
     * 离职 查询档案信息  分页
     */
    @Override
    public ToJson<HrStaffInfo> statusTwo(HttpServletRequest request, HttpServletResponse response, HrStaffInfo hrStaffInfo, String deptIds, Integer page, Integer pageSize, boolean useFlag, String export) {
        ToJson<HrStaffInfo> json = new ToJson<HrStaffInfo>();
        PageParams pages = new PageParams();
        pages.setPage(page);
        pages.setPageSize(pageSize);
        pages.setUseFlag(useFlag);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("page", pages);
        if (deptIds != null) {
            if (!StringUtils.checkNull(deptIds.toString())) {
                String fromIds = deptIds.toString();
                String[] substring = fromIds.split(",");
                map.put("deptIds", substring);
            }
        }
        if (hrStaffInfo.getJobPosition() != null & !StringUtils.checkNull(hrStaffInfo.getJobPosition())) {
            if (hrStaffInfo.getJobPosition().equals("null")) {
                hrStaffInfo.setJobPosition(null);
            }
        }
        if (hrStaffInfo.getUserPriv() != null & !StringUtils.checkNull(hrStaffInfo.getUserPriv())) {
            if (hrStaffInfo.getUserPriv().equals("undefined")) {
                hrStaffInfo.setUserPriv(null);
            }
        }
        map.put("hrStaffInfo", hrStaffInfo);
        try {
            if (export == null) {
                export = "0";
            }
            List<HrStaffInfo> list = hrStaffInfoMapper.statusTwo(map);
            if (list.size() > 0) {
                for (HrStaffInfo list1 : list) {
                    //政治面貌
                    HrCode hr_staff_contract1 = hrCodeMapper.selectCode("STAFF_POLITICAL_STATUS", list1.getStaffPoliticalStatus());
                    if (hr_staff_contract1 != null) {
                        list1.setStaffPoliticalStatusName(hr_staff_contract1.getCodeName());
                    } else {
                        hrStaffInfo.setStaffPoliticalStatusName(" ");
                    }
                    //籍贯
                    HrCode hr_staff_contract2 = hrCodeMapper.selectCode("AREA", list1.getStaffNativePlace());
                    if (hr_staff_contract2 != null) {
                        list1.setStaffNativePlaceName(hr_staff_contract2.getCodeName());
                    } else {
                        list1.setStaffNativePlaceName(" ");
                    }
                }
                json.setTotleNum(pages.getTotal());
                json.setObj(list);
                json.setMsg("ok");
                json.setFlag(0);
            } else {
                json.setMsg("档案信息为空");
            }
            if (export.equals("1")) {  //导出
                if (list != null) {
                    for (HrStaffInfo list1 : list) {
                        if ("0".equals(list1.getStaffMaritalStatus())) {
                            list1.setStatusName("未婚");
                        }
                        if ("1".equals(list1.getStaffMaritalStatus())) {
                            list1.setStatusName("已婚");
                        }
                        if ("2".equals(list1.getStaffMaritalStatus())) {
                            list1.setStatusName("离异");
                        }
                        if ("3".equals(list1.getStaffMaritalStatus())) {
                            list1.setStatusName("丧偶");
                        } else {
                            list1.setStatusName(" ");
                        }
                        if ("0".equals(list1.getStaffSex())) {
                            list1.setStaffSexName("男");
                        } else {
                            list1.setStaffSexName("女");
                        }
                        //学历
                        HrCode hr_staff_contract1 = hrCodeMapper.selectCode("STAFF_HIGHEST_SCHOOL", list1.getStaffHighestSchool());
                        if (hr_staff_contract1 != null) {
                            list1.setStaffHighestSchoolName(hr_staff_contract1.getCodeName());
                        } else {
                            list1.setStaffHighestSchoolName(" ");
                        }
                        //在职状态
                        HrCode hr_staff_contract = hrCodeMapper.selectCode("WORK_STATUS", list1.getWorkStatus());
                        if (hr_staff_contract != null) {
                            list1.setWorkStatusName(hr_staff_contract.getCodeName());
                        } else {
                            list1.setWorkStatusName(" ");
                        }
                    }
                    HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("人事档案", 14);
                    String[] secondTitles = {"部门", "姓名", "角色", "性别", "年龄", "出生日期", "手机号码", "学历", "毕业院校", "在职状态", "专业", "学位", "户口所在地", "婚姻状况"};
                    HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);
                    String[] beanProperty = {"deptName", "staffName", "userPrivName", "staffSexName", "staffAge", "staffBirth", "staffMobile", "staffHighestSchoolName", "graduationSchool", "workStatusName", "staffMajor", "staffHighestDegree", "staffDomicilePlace", "statusName"};
                    HSSFWorkbook workbook = null;
                    workbook = ExcelUtil.exportExcelData(workbook2, list, beanProperty);
                    response.setContentType("text/html;charser=UTF-8");
                    ServletOutputStream out = response.getOutputStream();
                    String filename = "人事档案.xls";//考虑多语言
                    filename = FileUtils.encodeDownloadFilename(filename,
                            request.getHeader("user-agent"));
                    response.setContentType("application/vnd.ms-excel");
                    response.setHeader("content-disposition",
                            "attachment;filename=" + filename);
                    workbook.write(out);
                    out.flush();
                    out.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }

    /**
     * 李阳
     * 根据staffId条件查询档案信息
     */
    @Override
    public ToJson<HrStaffInfo> getByStaffId(Integer staffId, String userId) {
        ToJson<HrStaffInfo> json = new ToJson<HrStaffInfo>();
        try {
            HrStaffInfo hrStaffInfo = hrStaffInfoMapper.getByStaffId(staffId, userId);
            if (hrStaffInfo != null) {
                //政治面貌
                HrCode hr_staff_contract1 = hrCodeMapper.selectCode("STAFF_POLITICAL_STATUS", hrStaffInfo.getStaffPoliticalStatus());
                if (hr_staff_contract1 != null) {
                    hrStaffInfo.setStaffPoliticalStatusName(hr_staff_contract1.getCodeName());
                } else {
                    hrStaffInfo.setStaffPoliticalStatusName(" ");
                }
                if (hrStaffInfo.getWorkJob() != null) {
                    UserJob userJob = positionManagementMapper.getUserjobId(Integer.parseInt(hrStaffInfo.getWorkJob()));
                    if (userJob != null) {
                        hrStaffInfo.setWorkJobStr(userJob.getJobName());
                    } else {
                        hrStaffInfo.setWorkJobStr("");
                    }
                }
                //籍贯
                HrCode hr_staff_contract2 = hrCodeMapper.selectCode("AREA", hrStaffInfo.getStaffNativePlace());
                if (hr_staff_contract2 != null) {
                    hrStaffInfo.setStaffNativePlaceName(hr_staff_contract2.getCodeName());
                } else {
                    hrStaffInfo.setStaffNativePlaceName(" ");
                }
                //职务
                if (hrStaffInfo.getJobPosition() != null) {
                    UserPost userPost = dutiesManagementMapper.getUserPostId(Integer.parseInt(hrStaffInfo.getJobPosition()));
                    if (userPost != null) {
                        hrStaffInfo.setJobPositionStr(userPost.getPostName());
                    } else {
                        hrStaffInfo.setJobPositionStr(" ");
                    }
                }
                //直属上级
                if (hrStaffInfo.getDirectlySuperior() != null && !StringUtils.checkNull(hrStaffInfo.getDirectlySuperior())) {
                    String str = hrStaffInfo.getDirectlySuperior().substring(0, hrStaffInfo.getDirectlySuperior().indexOf(","));
                    Users users = usersMapper.findUsersByuserId(str);
                    if (users != null) {
                        hrStaffInfo.setDirectlySuperiorName(users.getUserName());
                    }
                }
                //直属下级
                if (hrStaffInfo.getDirectlyUnder() != null && !StringUtils.checkNull(hrStaffInfo.getDirectlyUnder())) {
                    String str2 = hrStaffInfo.getDirectlySuperior().substring(0, hrStaffInfo.getDirectlyUnder().indexOf(","));
                    Users users = usersMapper.findUsersByuserId(str2);
                    if (users != null) {
                        hrStaffInfo.setDirectlyUnderName(users.getUserName());
                    }
                }
                json.setObject(hrStaffInfo);
                json.setMsg("ok");
                json.setFlag(0);
            } else {
                json.setObject(null);
                json.setFlag(0);
                json.setMsg("该用户没有建档");
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg("档案信息为空");
            json.setFlag(1);
        }
        return json;
    }

    /**
     * 李阳
     * 删除全部档案信息
     */
    @Override
    public ToJson<HrStaffInfo> delAll(String workStatus) {
        ToJson<HrStaffInfo> json = new ToJson<HrStaffInfo>();
        try {
            int line = hrStaffInfoMapper.delAll(workStatus);
            if (line > 0) {
                json.setMsg("ok");
                json.setFlag(0);
            } else {
                json.setMsg("档案信息为空");
                json.setFlag(0);
            }
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }

    @Override
    public ToJson<HrStaffInfo> selectLose(String workStatus) {
        ToJson<HrStaffInfo> json = new ToJson<HrStaffInfo>();
        try {
            List<HrStaffInfo> list = hrStaffInfoMapper.selecLose(workStatus);
            int line = 0;
            if (list.size() > 0) {
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getStaffNo() != null & list.get(i).getStaffNo() != "") {
                        line += hrStaffInfoMapper.delLose(Integer.parseInt(list.get(i).getUserId()));
                    }
                }
            } else {
                json.setMsg("没有废弃档案");
            }
            if (line > 0) {
                json.setMsg("ok");
            }
            json.setFlag(0);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }


    @Override
    public ToJson<HrStaffInfo> getByDeptIdStatusTwo(String deptId) {
        ToJson<HrStaffInfo> json = new ToJson<HrStaffInfo>();
        try {
            List<HrStaffInfo> list = hrStaffInfoMapper.getByDeptIdStatusTWO(deptId);
            if (list.size() > 0) {
                json.setObj(list);
            }
            json.setMsg("true");
            json.setFlag(0);
        } catch (Exception e) {
            e.printStackTrace();
            json.setMsg("flase");
            json.setFlag(1);
        }
        return json;
    }


    /**
     * 创建者：戚中校
     * 方法作用：根据条件查询数量
     *
     * @param
     * @param deptId(传入的多个部门的编号)
     * @return 返回的json字符串中  nums是返回的结果的数量
     */
    @Override
    public ToJson<HrStaffInfo> selectNumByCon(HrStaffInfo record, Integer[] deptId) {
        ToJson toJson = new ToJson();
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            if (deptId != null && deptId.length > 0) {
                map.put("deptId", deptId);
            }
            map.put("record", record);
            List<HrStaffInfo> list = hrStaffInfoMapper.selectNumByCon(map);
            for (HrStaffInfo hrStaffInfo : list) {
                if (!StringUtils.checkNull(hrStaffInfo.getStaffHighestSchool())) {
                    if (hrStaffInfo.getStaffHighestSchool().equals("1")) {
                        hrStaffInfo.setStaffHighestSchoolName("小学");
                    } else if (hrStaffInfo.getStaffHighestSchool().equals("2")) {
                        hrStaffInfo.setStaffHighestSchoolName("初中");
                    } else if (hrStaffInfo.getStaffHighestSchool().equals("3")) {
                        hrStaffInfo.setStaffHighestSchoolName("高中");
                    } else if (hrStaffInfo.getStaffHighestSchool().equals("4")) {
                        hrStaffInfo.setStaffHighestSchoolName("中专");
                    } else if (hrStaffInfo.getStaffHighestSchool().equals("5")) {
                        hrStaffInfo.setStaffHighestSchoolName("大专");
                    } else if (hrStaffInfo.getStaffHighestSchool().equals("6")) {
                        hrStaffInfo.setStaffHighestSchoolName("本科");
                    } else if (hrStaffInfo.getStaffHighestSchool().equals("7")) {
                        hrStaffInfo.setStaffHighestSchoolName("研究生");
                    } else if (hrStaffInfo.getStaffHighestSchool().equals("8")) {
                        hrStaffInfo.setStaffHighestSchoolName("博士");
                    } else if (hrStaffInfo.getStaffHighestSchool().equals("9")) {
                        hrStaffInfo.setStaffHighestSchoolName("博士后");
                    } else if (hrStaffInfo.getStaffHighestSchool().equals("11")) {
                        hrStaffInfo.setStaffHighestSchoolName("技校");
                    } else {
                        hrStaffInfo.setStaffHighestSchoolName("其它");
                    }
                }
                if (!StringUtils.checkNull(hrStaffInfo.getStaffHighestDegree())) {
                    if (hrStaffInfo.getStaffHighestDegree().equals("1")) {
                        hrStaffInfo.setStaffHighestDegreeName("博士后");
                    } else if (hrStaffInfo.getStaffHighestDegree().equals("2")) {
                        hrStaffInfo.setStaffHighestDegreeName("博士");
                    } else if (hrStaffInfo.getStaffHighestDegree().equals("3")) {
                        hrStaffInfo.setStaffHighestDegreeName("MBA");
                    } else if (hrStaffInfo.getStaffHighestDegree().equals("4")) {
                        hrStaffInfo.setStaffHighestDegreeName("硕士");
                    } else if (hrStaffInfo.getStaffHighestDegree().equals("5")) {
                        hrStaffInfo.setStaffHighestDegreeName("双学士");
                    } else if (hrStaffInfo.getStaffHighestDegree().equals("6")) {
                        hrStaffInfo.setStaffHighestDegreeName("学士");
                    } else if (hrStaffInfo.getStaffHighestDegree().equals("7")) {
                        hrStaffInfo.setStaffHighestDegreeName("无");
                    } else {
                        hrStaffInfo.setStaffHighestDegreeName("其它");
                    }
                }
                if (!StringUtils.checkNull(hrStaffInfo.getStaffSex())) {
                    if (hrStaffInfo.getStaffSex().equals("0")) {
                        hrStaffInfo.setStaffSexName("男");
                    } else if (hrStaffInfo.getStaffSex().equals("1")) {
                        hrStaffInfo.setStaffSexName("女");
                    } else {
                        hrStaffInfo.setStaffSexName("未知");
                    }
                }
                if (!StringUtils.checkNull(hrStaffInfo.getStaffPoliticalStatus())) {
                    if (hrStaffInfo.getStaffPoliticalStatus().equals("1")) {
                        hrStaffInfo.setStaffPoliticalStatusName("群众");
                    } else if (hrStaffInfo.getStaffPoliticalStatus().equals("2")) {
                        hrStaffInfo.setStaffPoliticalStatusName("共青团员");
                    } else if (hrStaffInfo.getStaffPoliticalStatus().equals("3")) {
                        hrStaffInfo.setStaffPoliticalStatusName("中共党员");
                    } else if (hrStaffInfo.getStaffPoliticalStatus().equals("4")) {
                        hrStaffInfo.setStaffPoliticalStatusName("中共预备党员");
                    } else if (hrStaffInfo.getStaffPoliticalStatus().equals("5")) {
                        hrStaffInfo.setStaffPoliticalStatusName("民主党派");
                    } else {
                        hrStaffInfo.setStaffPoliticalStatusName("无党派人士");
                    }
                }

                if (!StringUtils.checkNull(hrStaffInfo.getWorkStatus())) {
                    if (hrStaffInfo.getWorkStatus().equals("01")) {
                        hrStaffInfo.setWorkStatusName("在职");
                    } else if (hrStaffInfo.getWorkStatus().equals("02")) {
                        hrStaffInfo.setWorkStatusName("离职");
                    } else if (hrStaffInfo.getWorkStatus().equals("03")) {
                        hrStaffInfo.setWorkStatusName("离休");
                    } else if (hrStaffInfo.getWorkStatus().equals("04")) {
                        hrStaffInfo.setWorkStatusName("退休");
                    } else if (hrStaffInfo.getWorkStatus().equals("05")) {
                        hrStaffInfo.setWorkStatusName("借调");
                    } else {
                        hrStaffInfo.setWorkStatusName("其它");
                    }
                }
                if (!StringUtils.checkNull(hrStaffInfo.getStaffOccupation())) {
                    if (hrStaffInfo.getStaffOccupation().equals("01")) {
                        hrStaffInfo.setStaffOccupationName("合同工");
                    } else if (hrStaffInfo.getStaffOccupation().equals("02")) {
                        hrStaffInfo.setStaffOccupationName("正式员工");
                    } else if (hrStaffInfo.getStaffOccupation().equals("03")) {
                        hrStaffInfo.setStaffOccupationName("临时工");
                    } else {
                        hrStaffInfo.setStaffOccupationName("其它");
                    }
                }

                //员工籍贯
                if (!StringUtils.checkNull(hrStaffInfo.getStaffNativePlace())) {
                    HrCode hr_staff_contract1 = hrCodeMapper.selectCode("AREA", hrStaffInfo.getStaffNativePlace());
                    if (hr_staff_contract1 != null) {
                        hrStaffInfo.setStaffNativePlaceName(hr_staff_contract1.getCodeName());
                    } else {
                        hrStaffInfo.setStaffNativePlaceName("其它");
                    }
                }
                //员工职称
                if (!StringUtils.checkNull(hrStaffInfo.getPresentPosition())) {
                    HrCode hr_staff_contract1 = hrCodeMapper.selectCode("PRESENT_POSITION", hrStaffInfo.getPresentPosition());
                    if (hr_staff_contract1 != null) {
                        hrStaffInfo.setPresentPositionName(hr_staff_contract1.getCodeName());
                    } else {
                        hrStaffInfo.setPresentPositionName("其它");
                    }
                }

                //员工类型
                if (!StringUtils.checkNull(hrStaffInfo.getStaffOccupation())) {
                    HrCode hr_staff_contract1 = hrCodeMapper.selectCode("STAFF_OCCUPATION", hrStaffInfo.getStaffOccupation());
                    if (hr_staff_contract1 != null) {
                        hrStaffInfo.setStaffOccupationName(hr_staff_contract1.getCodeName());
                    } else {
                        hrStaffInfo.setStaffOccupationName("其它");
                    }
                }


            }

            if (list.size() >= 0) {
                toJson.setObj(list);
                toJson.setMsg("ok");
                toJson.setFlag(0);
            } else {
                toJson.setMsg("err");
                toJson.setFlag(1);
            }

        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg("err");
            toJson.setFlag(1);
        }
        return toJson;
    }

    public ToJson<HrStaffInfo> getNoDept() {
        ToJson<HrStaffInfo> json = new ToJson<HrStaffInfo>();
        try {
            List<HrStaffInfo> list = hrStaffInfoMapper.getNoDept();
            if (list.size() > 0) {
                json.setMsg("true");
                json.setFlag(1);
                json.setObj(list);
            }

        } catch (Exception e) {
            e.printStackTrace();
            ;
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }

    /**
     * @Description: 查询当前用户信息
     * @Author: xiaomeng.Yu
     * @Date: 2018/8/8
     */
    @Override
    public ToJson<Object> selectStaffInfo(String userId) {
        ToJson<Object> json = new ToJson<Object>();
        try {
            if (userId != null) {
                HrStaffInfo hrStaffInfo = hrStaffInfoMapper.selectStaffInfo(userId);
                if (hrStaffInfo != null) {
                    hrStaffInfo.setDeptName(departmentMapper.getDeptNameByDeptId(hrStaffInfo.getDeptId()));
                /*  if(hrStaffInfo.getDeptId()!=null){
                       //根据部门id查询部门名称
                      String deptNameByDeptId = departmentMapper.getDeptNameByDeptId(hrStaffInfo.getDeptId());
                  }*/
                    json.setFlag(0);
                    json.setMsg("ok");
                    json.setObject(hrStaffInfo);
                } else {
                    json.setFlag(0);
                    json.setMsg("数据为空");
                }

            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg("err");
            json.setObject(null);
        }
        return json;
    }

    @Override
    public ToJson deletPhoteName(HrStaffInfo hrStaffInfo) {
        ToJson toJson = new ToJson();
        try {
            if (hrStaffInfo.getPhotoName() == null) {
                hrStaffInfo.setPhotoName("");
            }
            int count = hrStaffInfoMapper.updatephoneName(hrStaffInfo);
            toJson.setMsg("true");
            toJson.setFlag(0);
        } catch (Exception e) {
            toJson.setMsg(e.getMessage());
            toJson.setFlag(1);
        }
        return toJson;
    }


    @Override
    public BaseWrapper uploadImg(HttpServletRequest request, MultipartFile file) {
        BaseWrapper wrapper = new BaseWrapper();
        try {
            Cookie redisSessionCookie = CookiesUtil.getCookieByName(request, "redisSessionId");
           /* Users usersTemp = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionCookie);
            if (usersTemp == null || usersTemp.getUid() == null) {
                wrapper.setFlag(false);
                wrapper.setMsg("用户信息不正确");
                return wrapper;
            }*/
            if (file != null) {
                String imageType = file.getContentType();
                boolean b = allowUpload(imageType);
                if (!b) {
                    wrapper.setFlag(false);
                    wrapper.setMsg("图片格式不正确");
                    return wrapper;
                }
                String fileName = file.getOriginalFilename();
                if (!FileUploadUtil.checkFilePicture(fileName)) {
                    wrapper.setFlag(false);
                    wrapper.setMsg("上传文件后缀名不可用");
                    return wrapper;
                }
            } else {
                wrapper.setFlag(false);
                wrapper.setMsg("图片不存在");
                return wrapper;
            }
            String realPath = request.getSession().getServletContext().getRealPath("/");
            String resourcePath = "ui/img/hr";
            Users users = new Users();
            //上传图片并进行修改数据库数据
            String fileName = FileUploadUtil.rename(file.getOriginalFilename());
            File dir = new File(realPath + resourcePath);
            if (!dir.exists()) {
                dir.mkdirs();
            }
            File realfile = new File(dir, fileName);
            file.transferTo(realfile);
            String fileName128 = FileUploadUtil.rename(fileName, "s");
            String newImg = FileUploadUtil.zipImageFile(realfile.getAbsolutePath(), 128, 128, 1, fileName128, dir.getAbsolutePath());
            wrapper.setFlag(true);
            wrapper.setData(fileName128);
            wrapper.setMsg(fileName);
//                users.setAvatar(filename128);
//                users.setAvatar128(fileName);
//            users.setUid(usersTemp.getUid());
//            int count = usersMapper.editUser(users);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return wrapper;
    }

}
